command line - Understanding a for loop in a batch file to rename files by replacing strings

06
2014-04
  • Deb
    @echo off
    Setlocal enabledelayedexpansion
    
    Set "Pattern=rename"
    Set "Replace=reuse"
    
    For %%a in (*.jpg) Do (
    Set "File=%%~a"
    Ren "%%a" "!File:%Pattern%=%Replace%!"
    )
    

    This renames .jpgs having substring 'rename'. Ref: http://stackoverflow.com/questions/16128869/how-to-rename-file-by-replacing-substring-using-batch. Can anyone make me understand how the for loop does this job? Additionally, is it possible to use a /f switch here, to get rid of 'Access Denied'? And where can I place the nul?

  • Answers
  • Rudu

    For %%a in (*.jpg) Do ( - says get a directory listing of each file with extension jpg and give me the current name in the variable %%a and do something (which follows between the brackets). I'm not sure where Access Denied would be coming from, presumably you lack file system rights to rename one or more files.

    The real effort of this command is coming from enabledelayedexpansion which allows variable replace syntax.

    Ren "%%a" "!File:%Pattern%=%Replace%!" - The rename part can be simplified into ren this that... the trick is that second block with exclamation marks... which starts variable replacement.

    "!File:%Pattern%=%Replace%!" - for variable File (set on line above to be full path, and filename of the current file in the for loop - which isn't necessary with this directory listing) replace all occurrences of Pattern with Replace. Pattern is set on line 5 to rename while Replace is set on line 6 to reuse so this command says: in the given filename replace all occurrences of the string rename with reuse.

    To compile it all:

    1. For all files with extension jpg in the current directory

    2. Rename the file so all occurrences of the string rename are now reuse

    for /f is for reading through the text content of one ore more files line by line - so no, it won't help you here.

    Where's this nul coming from?


  • Related Question

    command line - How to distinguish folders from files in a batch script? (Or how to copy & rename-timestamp them indiscriminately?)
  • fluxtendu

    I want to create a batch file to copy files or folders in a specified directory and append the date and time to their names.
    Here's my actual code:

    @echo off
    Set _bpath=T:\Backup\
    if [%1]==[] goto :eof
    :loop
    Set _file=%~n1%
    Set _ext=%~x1%
    For /f "tokens=1-3 delims=1234567890 " %%a in ("%time%") Do Set "delims=%%a%%b%%c"
    For /f "tokens=1-4 delims=%delims%" %%G in ("%time%") Do (
       Set _hh=%%G
       Set _min=%%H
       Set _ss=%%I
       Set _ms=%%J
    )
    copy %1 "%_bpath%%_file%(%date:/=-% %_hh%h%_min%m%_ss%s)%_ext%"
    shift
    if not [%1]==[] goto loop
    

    This one works for files only and I could adapt it easily for folders with xcopy /E %1 "%_bpath%%_file%(%date:/=-% %_hh%h%_min%m%_ss%s) but I would like to avoid dealing with two batch files.

    So how to copy/rename them indiscriminately (I have try with copy, xcopy and robocopy without success) or how to distinguish them to create two IF branch? (Using %~x1% is too unreliable...)


  • Related Answers
  • asdfg

    This might be useful to you, see this question