How to Use Batch File For Loop in Windows

How to Use Batch File For Loop to Create Relative Paths?

One of the challenges you may face when using a batch file for loop is how to create a relative path of the file or folder processed by the loop. A relative path is a path that does not contain the initial drive or directory name, but only a part of a different path from the current directory.

For example, if the current directory is C:\Users\Alice\Documents, then the relative path of the file C:\Users\Alice\Pictures\image.jpg is …\Pictures\image.jpg.For example, if the current directory is C:\Users\Alice\Documents, then the relative path of the file C:\Users\Alice\Pictures\image.jpg is …\Pictures\image.jpg.

Creating relative paths can be useful if you want to move, copy or rename files or folders to a different location, but keep the original directory structure. For example, you might want to move all text files from the C:\Users\Alice\Documents directory and its subdirectories to the D:\Backup directory, but keep the subdirectories.

To create relative paths with batch for loop files, you can use some of the following techniques:

  • Uses the environment variable “CD” to obtain the current directory, and removes it from the full path of the file or folder by using the “SET” command.
  • Use the /V:ON option to enable delayed variable expansion, and use an exclamation mark (!) as a variable delimiter instead of a percent sign (%). This is useful if you want to change the value of a variable in a loop.
  • Uses the commands “PUSHD” and “POPD” to change the current directory while in the loop, and uses a period (.) to refer to the current directory.

Example:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R C:\Users\Alice\Documents %%G IN (*.txt) DO (
  SET "source=%%G"
  SET "destination=!source:%CD%=!"
  ECHO !destination!
)
ENDLOCAL

The above command will search for all text files in the C:\Users\Alice\Documents directory and its subdirectories, and display their relative paths from the current directory. For example, if the current directory is C:\Users\Alice\Documents, then the relative path of the file C:\Users\Alice\Documents\Folder1\File1.txt is Folder1\File1.txt.

@ECHO OFF
FOR /R C:\Users\Alice\Documents %%H IN (*.txt) DO (
  PUSHD "%%~dpH"
  SET "source=%%H"
  SET "destination=%CD%\%%~nxH"
  ECHO %destination%
  POPD
)

The above command will search for all text files in the C:\Users\Alice\Documents directory and its subdirectories, and display their full path using the current directory as a prefix. For example, if the current directory is D:\Backup, then the full path of the file C:\Users\Alice\Documents\Folder1\File1.txt is D:\Backup\Folder1\File1.txt.

Conclusion

Batch file for loop is a very useful feature for creating more efficient and flexible batch files. By using a for loop, you can repeat a specific command for each element in a data set, such as a list of values, a range of numbers, or the result of another command.

You can also use some of the additional options available for for loops, such as /F, /R, and /D, to process text files, subfolders, or directories. In addition, you can also access information about the files and folders processed by the loop, such as name, extension, size, modification date, attributes, and path, by using path modification on loop variables.

Finally, you can also create a relative path of the files and folders processed by the loop, by using several techniques such as removing the current directory from the complete path, enabling delayed variable expansion, or changing the current directory while in the loop.

Hopefully, this article is helpful for those of you who want to learn more about batch files for loops in Windows. If you have any questions or suggestions, please leave a comment below. Thank you for reading.

Latest Articles