title: how to create batch file for diskpart cmd in windows which can lock (read only) and unlock the drive
body: <p>I am trying to create batch files to switch on/off read-only mode of drives.</p>
  
  <p>I have created two batch files which call diskpart, and two text files that contain the commands to be passed to diskpart.</p>
  
  <p>When I run the diskpart commands separately, they work; But when I run them through the batch file and text files, they don't work.</p>
  
  <hr>
  
  <h1>Batch-file to lock disk</h1>
  
  <pre><code>diskpart /s readonlyfile.txt &gt; logfile .txt
  pause
  </code></pre>
  
  <hr>
  
  <h1>Batch-file to unlock disk</h1>
  
  <pre><code>diskpart /s offreadonlyfile.txt
  pause 
  </code></pre>
  
  <hr>
  
  <h1><code>readonlyfile.txt</code> (for locking the disk)</h1>
  
  <pre><code>diskpart 
  list disk
  select disk 1 
  attributes disk set readonly 
  </code></pre>
  
  <hr>
  
  <h1><code>offreadonlyfile.txt</code> (for unlocking the disk)</h1>
  
  <pre><code>diskpart 
  list disk
  select disk 1 
  attributes disk clear readonly
  </code></pre>
  
  <hr>