On a PC with Windows 7, I'm using a simple batch script to rename some Excel files, pre-pending their parent folder name:
for /f "delims=" %%i in ('dir /b /AD') do (
cd "%%i"
for /f "delims=" %%j in ('dir /b *.xl*') do ren "%%~j" "%%i_%%~j"
cd..
)
I noticed that some files generated an error, "System cannot find the specified file." These files all had an em dash (—) in the filename. After I changed the em dash to a regular hyphen (-), using Windows Explorer, the script worked fine on these files.
How can I help script the rename of these files?
Your problem is not with the batch variables: from the command line this works fine:
for %i in (*) do ren "%~i" "test_%~i"
but, as can be seen from:
for /f "delims=" %i in ('dir /b /a-d') do #echo ren "%~i" "test2_%~i"
dir /b is changing the em dash to a hyphen, and so the ren command clearly won't find the file to change.
For your examples you should find:
for /d %%i in (*) do (
and
for %%j in (*.xl*) do ...
should work fine.
If you need the dir /b for other reasons, I don't see a solution right now.
(I had a convoluted attempt exchanging all hyphens for question marks, using the "Environment variable substitution" and "delayed environment variable expansion" as described in SET /? and CMD /?, allowing any character to match, and then again use ren pattern matching to ignore the problem.
I.e.
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%I in ('dir /b /a-d') do (
Set K=%%I
ren "!K:-=?!" "test2_!K:-=?!"
)
but ren * x* replaces the start of the files with x, so the above replaces the hyphens with the content at that location before test_ was inserted.
So the best this approach can do is convert the em dashes to hyphens with:
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%I in ('dir /b /a-d') do (
Set K=%%I
ren "!K:-=?!" "test2_!K!"
)
)
And confirming it is the output of dir /b that is the problem: on the command line:
dir /b *—* > test.txt
where that — is an em dash, will only list the files with em dashes, but, in the output file, e.g. Notepad test.txt, you'll only find hyphens, and no em dashes.
BTW I've done all this testing on Windows 8.1 which VER displays as Microsoft Windows [Version 6.3.9600].
(As I mention above I did have ren * x* in this answer, but that replaces the first character with x rather than insert it, which I always thought ren did!)
Related
I have went through a lot of guides for it, but havent found a way to do something similar to lets say turn all files in folder, like these files:
djhwu4s_cat_ruhg29.png
397y_dog_j0929_ej93.png
8yhh_owl.png
into these:
_cat.png
_dog.png
_owl.png
So basically removing everything from file names but a list of predefined strings i am searching for. In example above i would define list as "_cat", "_dog", "_owl". I know that each file will have only one of these variables, and there will be only one file with each of them in folder.
Will appreciate any tips on how to achieve that. Thanks in advance!
edit:
Here is what i came up with (with stuff i can understund) and what seems to be working fine now.
#echo off
setlocal enabledelayedexpansion
set v1=_cat-cat
set v2=_cat-owl
set v3=_cat
set v4=_dog
set v5=_owl
set v6=_horse
FOR /L %%a IN (1,1,6) DO (
rem echo %%a
rem echo !v%%a!
FOR /f %%f in ('dir /b /a:-D *!v%%a!.*') DO (
REN %%f !v%%a!.*
)
FOR /f %%f in ('dir /b /a:-D *!v%%a!_*.*') DO (
REN %%f !v%%a!.*
)
)
rem using two passes of this simpler code i can grasp and understund with dot and with underscore
rem after constructed variables value i make sure cat-cat is not recognised as and renamed to cat
rem no matter if im getting file with that variable as the last string before extension or another underscore
rem Gonna test it in combat now
For some reason this stuff doesnt work with files containing spaces and characters like:
"ab’c efg_dog.png"
FOR /L %%a IN (1,1,36) DO (
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
REN "%%f" "!v%%a!.*"
)
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
REN "%%f" "!v%%a!.*"
)
)
After further testing i have realised the problem starts with the %%f, and not the REN function as i thought. echo %%f before ren gives just the first part of the name to the first space, hence the REN function cant find the file. In case of "ab’c efg_dog.png" after finding the file with dir, the %%f becomes just "ab’c".
edit: After more tests and experiments and adding those "delims" to the code, the echo now shows proper, full names to be renamed, but it replaces that weird ’ character with ' for the REN command and thats why it still cant find the file to rename.
FOR /L %%a IN (1,1,36) DO (
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
)
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"
FOR /f "delims=" %%e IN ('dir /b /a-d "*_*" 2^nul ^|findstr /v /b "_"') DO (
FOR /f "tokens=2delims=_." %%y IN ("%%e") DO ECHO REN "%%e" "_%%y%%~xe"
)
POPD
GOTO :EOF
For lack of examples, here's a start.
Always verify against a test directory before applying to real data.
Process the list of filenames that match *_*; find those names that do not start _, pick the second string between the delimiters _ and . and rename using that string (in %%y), prefixed by _ and appended with the original extension.
The ren command is simply echoed to the screen for verification. When happy, remove the echo before the ren to actually execute the rename.
I am trying to copy some pictures from one folder and move it to another folder using batch. I am having trouble in setting the path since the path contains spaces in it.If I remove the spaces from folder it works fine but with spaces it gives error that cannot find the path.. here is the code.
#echo off
SET odrive=%odrive:~0,2%
setlocal enabledelayedexpansion
set backupcmd=echo
set backupcmd=xcopy /s /c /d /e /h /i /r /y
set "filesw=C:\Users\%USERNAME%\Numerical Analysis\*.png"
for /f "delims=" %%i in ('dir /s /b %filesw%') do (
if "%%~xi"==".pdf" set "dest=D"
if "%%~xi"==".docx" set "dest=P"
if "%%~xi"==".zip" set "dest=Z"
if "%%~xi"==".rar" set "dest=Z"
if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
%backupcmd% "%%i" "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!\"
)
#echo off
cls
It would be really helpful if you guys help me fix this path problem.
The solution to your problem, specifically "setting the path since the path contains spaces in it" leading to "cannot find the path" is to "quote the filespec" Thus:
for /f "delims=" %%i in ('dir /s /b "%filesw%"') do (
In this way, the string between the double-quotes is used literally (although certain special characters with a meaning to batch like &^)! need to be escaped; that is, preceded by a caret ^). As you have it, the dir command will be executed with multiple arguments since spaces are separators and the variable filesw will be substituted literally into the dir command before execution - and like most commands, dir uses spaces (commas, tabs, semicolons) as separators.
This is how I would do it. Use the FOR /R command to walk the directory tree looking for the file type you want. But I am just guessing at what you are trying to do.
#echo off
setlocal enabledelayedexpansion
set backupcmd=xcopy /s /c /d /e /h /i /r /y
set "filep=C:\Users\%USERNAME%\Numerical Analysis"
for /R "%filep%" %%i in (.) do (
if "%%~xi"==".pdf" set "dest=D"
if "%%~xi"==".docx" set "dest=P"
if "%%~xi"==".zip" set "dest=Z"
if "%%~xi"==".rar" set "dest=Z"
if "%%~di"=="C:" if "!dest!"=="Z" set "dest=!dest!3"
%backupcmd% "%%i" "%drive%\Personal\PICS\Wedding\Barat\MOVIE!dest!\"
)
I'm trying to figure out how to match folders with a dot in the file name (e.g., ".svn") in a Windows batch script.
Here's the basic script:
setlocal
pushd c:\myDir
#echo off
FOR /D /r %%G in ("*\.whatever") DO (
echo %%G
REM do stuff
)
#echo on
popd
endlocal
This works just fine for most folder names (e.g., "*bin"), but I can't figure out the method to specify a folder with the dot. "*.whatever" and "*\.whatever" return no results. I'm guessing I'm missing some escape character or something equally simple, but I haven't been able to find any documentation on it.
(Before anyone asks, no I'm not trying to recursively delete subversion folders; "*.svn" is just an example.)
Maybe I am missing something, but as you say it seems simple
for /r /d %%a in (.*) do echo %%~fa
But if the folders are hidden, the normal for will not be able to see them, so we need to execute a dir command an process its output with a for /f
for /f "delims=" %%a in ('dir /ad /s /b .*') do echo %%~fa
I want to remove the part of a filename after the third "_" from thousand of files. The structure after the third "_" varies and contains "_" in some cases. The length of the first part varies so I can't just remove the first 15 characters. The result should be unique.
The filenames look like this:
00_TEXT_=Text00._AA1234L_AA1_1.pdf
00_TEX_=Text00._AA1234L_AA1_2.pdf
00_TEXT_=TextText00._DD2023A.pdf
00_TEXT_=Text00._AA2345L_BB1_1.pdf
00_TEXT_=Text00._AA2345L_BB1_2.pdf
The result should look like this:
AA1234L_AA1_1.pdf
AA1234L_AA1_2.pdf
DD2023A.pdf
AA2345L_BB1_1.pdf
AA2345L_BB1_2.pdf
Any idea why this is not working:
#echo off
setlocal enabledelayedexpansion
set deletestring=*_*_*_
for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)
I was able to get it working with this:
#echo off
setlocal enabledelayedexpansion
set deletestring=*_*_*_*
for /f "tokens=1,2,3,* delims=_" %%F in ('dir /b "%deletestring%"') do (
Ren "%%F_%%G_%%H_%%I" "%%I"
)
endlocal
Note that enabledelayedexpansion isn't really needed in the above.
Alternately, you could do this as a single line (no batch file needed):
for /f "tokens=1,2,3,* delims=_" %F in ('dir /b "*_*_*_*"') do Ren "%F_%G_%H_%I" "%I"
The idea is to simply split the matching filenames apart by underscores and then reconstruct the names during the rename process (%%F_%%G_%%H_%%I gives the original file name when going through the loop). Then rename the file to everything after the 3rd underscore, which is the %%I value.
Your FINDSTR search is wrong - a string of any characters (wildcard) is .*, not *.
Variable find/replace does not support wildcards, except for the !var:*search=! syntax that replaces everthing up until the first occurrence of "search".
There is no need for FINDSTR, all you need is DIR with normal wildcard masking.
You can use FOR /F to parse the name into tokens. I use two loops - the first to get the entire name, and the second to parse out the portion after the 3rd _.
The following should work:
#echo off
for /f "eol=: delims=" %%A in (
'dir /b /a-d *_*_*_*'
) do for /f "tokens=3* delims=_" %%B in ("%%A") do ren "%%A" "%%C"
Or you could use my jren.bat utility that renames files using regular expression replacement. It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
jren "^(.*?_){3}" ""
Use CALL JREN if you put the command within another batch script.
In a Windows cmd batch script named my.bat, I want to execute a command for all the files in the current directory except for the batch file my.bat.
I use below command in my.bat currently to run the command only for *.txt *.ppt, but really as new files of different extensions might be added to the folder and hence execution of this command by excluding one type of file extension (in my case *.bat) would be more readable/helpful.
FOR /F "delims=|" %%i IN ('dir /b *.txt *.ppt') DO echo %%i
Question is: How do I exclude that file alone with that particular file extension and make the for command execute on all files of all extensions except the excluded one?
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do if not %%~xi==.bat echo %%i
I have added tokens=* in as well otherwise you won't get full filenames if they have spaces.
To echo without the dot
setlocal enabledelayedexpansion
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do (
set e=%%~xi
set e=!e:.=!
echo !e!
)
This is providing that the file doesn't have any other dots, otherwise it will remove them too. This is a bit more sturdy than just removing the 4th character from the end though, as not all files have a 3 character extension.
You could pass the output of the dir /b command through findstr, like so:
FOR /F "delims=|" %%i IN ('dir /b ^| findstr /v /r "^my.bat$"') DO echo %%i
The /v option to findstr prints anything that doesn't match the parameter. The match is based on a regular expression that matches only lines that contain my.bat and nothing else.