I have several .pdf and .tex-files in a certain folder "ABC".

They all are named by the scheme

and so forth

C in this scheme stands for chapter, S for section and SS for subsection. So in the folder ABC I also have the folder for each chapter, simply named C01, C02 and so forth. In each chapter folder I have one folder named Pictures.

Could someone please write a batch script so I could move all .tex and .pdf files in their respective Pictures-folder, C04S01SS02_More_words.pdf would get moved to C04/Pictures and C01S02_Word.pdf to C01/Pictures and so forth?

Note: of course I googled this already but so far I was unable to convert all the many bits into a working script. I do not want to do T&E runs anymore. :/


Create this batch file:

@echo off
for %%F in (*.pdf *.tex) do call :subr "%%F"
exit /b

set filename=%1
move %1 %filename:~1,3%\Pictures
exit /b


  • for %%F in (*.pdf *.tex) do … performs the following statement once for each filename that matches *.pdf or *.tex, with %%F set to the filename.
  • call :subr "%%F" invokes the bottom three lines of the script, with the first argument (%1) set to "%%F" (the filename, quoted).
  • exit /b exits the script without blowing away the Command Prompt window.
  • set filename=%1 copies the filename argument into the filename variable.
  • move moves a file.
  • %variable:~start,len% takes the indicated substring of %variable% (the first character is #0), so %filename:~1,3% is the first three characters of %filename%.  (The count starts at 0; I’m using 1 to skip the ".)
  • exit /b returns from the subroutine.
  • Thank you for your reply. Hm, does this account for Pictures folder? Do I just have to write %filename:~0,3%/Pictures for that? – henry Aug 14 '14 at 5:46
  • The FOR loop does not work. It only works on one file. – henry Aug 14 '14 at 6:02
  • I think it has to do with spaces in the filename. If I remove the spaces in a filename, it works with said file. – henry Aug 14 '14 at 6:13
  • That being said, I can't figure out how to use delims= here since this code snippet is a bit different from all theothers ones I found on the net – henry Aug 14 '14 at 7:11
  • 1
    D’oh! I misread the question. Change %filename:~0,3% to %filename:~0,3%\Pictures. And d’oh again! You can handle filenames with space characters in them by changing %%F to "%%F" and then changing %filename:~0,3%\Pictures to %filename:~1,3%\Pictures. You shouldn’t need delims=. – G-Man Says 'Reinstate Monica' Aug 14 '14 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.