|
Answer» I have a DIRECTORY with filenames that have a % (percent sign) in the name. I need to rename all files with "%" sign to an "_" underscore. Any ideas how I can create a loop to do this in DOS batch [not VB]?
The code below will convert spaces to underscores, not sure how to handle percent signs.
@echo off & setlocal enableextensions pushd "%ENCRYPTED_PATH%" for /f "tokens=*" %%f in (' dir /b /a-d-s "%ENCRYPTED_PATH%\* *.*"') do ( set "_=%%~nxf" call ren "%%~ff" "%%_: =_%%" ) popd endlocalI thought percent signs are illegal in Windows filenames, just how did these files get on your system? The following should replace a percent sign with a # in each filename encountered. Remove echo when happy. I have not got any files with % in the name so I created a DUMMY set in a text file but this should work...
Code: [Select]@echo off setlocal enabledelayedexpansion dir /b /a-d-s "%ENCRYPTED_PATH%\* *.*" > test.txt for /f "delims=" %%A in (test.txt) do ( set string1=%%A set string2=!string1:%%=#! echo ren "!string1!" "!string2!" ) In cmd scripts ("batch files") you need to escape certain special characters. For many symbols the escape char is a CARET (^) but for a percent sign the escape character is... a percent sign. So double them up.
Quote from: Salmon Trout on October 27, 2009, 01:40:56 PM I thought percent signs are illegal in Windows filenames, just how did these files get on your system?
The files are coming from some vendor, its the way they always create all the files on their sftp SERVER. Whatever happened with the simple naming standards.
Thanks for the code....
|