For some reason my code below only works if the batch file is in the same folder as the files to be renamed even though i've specified the path. When the batch file is in a different folder I receive an error saying the file can't be found. Any input on this?
#echo off&setlocal
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
for /f "delims=" %%a in ('dir C:\Users\%username%\Downloads\Export_*.csv /b /a-d /o-d') do (
set "fname=%%~a"
set /a counter+=1
SETLOCAL ENABLEDELAYEDEXPANSION
call set "nname=%%name!counter!%%"
ren "!fname!" "!nname!%%~xa"
endlocal
)
just add a working path:
#echo off&setlocal
set "workingpath=%userprofile%\Downloads"
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
for /f "delims=" %%a in ('dir "%workingpath%\Export_*.csv" /b /a-d /o-d') do (
set "fname=%%~a"
set /a counter+=1
SETLOCAL ENABLEDELAYEDEXPANSION
call set "nname=%%name!counter!%%"
ren "%workingpath%\!fname!" "!nname!%%~xa"
endlocal
)
Endoro has a good working solution for the stated problem. Another option is to simply PUSHD to where the files are located. Then you no longer need to include the path in the remainder of the code.
Other points unrelated to the question:
It is probably a good idea to initialize counter to 0, just in case some other process already set the value to a number.
You don't really need the nname variable.
I prefer to transfer the counter value to a FOR variable so that I don't need to use the CALL construct. (For those that don't know, the delayed expansion toggling is to protect ! characters that may be in the file name).
#echo off
setlocal
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
pushd "C:\Users\%username%\Downloads"
set /a counter=0
for /f "delims=" %%a in ('dir Export_*.csv /b /a-d /o-d') do (
set "fname=%%~a"
set /a counter+=1
setlocal enableDelayedExpansion
for %%N in (!counter!) do (
endlocal
ren "!fname!" "!name%%N!.csv"
)
)
popd
Finally, FINDSTR with the /N option can eliminate the need for CALL or additional FOR
#echo off
setlocal
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
pushd "C:\Users\%username%\Downloads"
for /f "tokens=1* delims=:" %%A in (
'dir Export_*.csv /b /a-d /o-d ^| findstr /n "^"'
) do (
set "fname=%%~B"
setlocal enableDelayedExpansion
ren "!fname!" "!name%%A!.csv"
endlocal
)
popd
#cbmanica is right: the directory is not being included in the variable fname, so you'll have to specify it manually in the ren command.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
set "dir=C:\Users\%username%\Downloads\"
for /f "delims=" %%a in ('dir %dir%Export_*.csv /b /a-d /o-d') do (
set "fname=%%~a"
set /a counter+=1
:: <Comment> In the below line is the use of "call" necessary? </Comment>
call set "nname=%%name!counter!%%"
ren "!dir!!fname!" "!dir!!nname!%%~xa"
)
endlocal
That should do exactly what you want.
Related
What I am trying to do is replace part of a file name with my computer name.
#echo off
set host=%COMPUTERNAME%
set host=%host:~4, -2%
for /f "delims=" %%a in ('dir /a:-d /o:n /b') do call :next "%%a"
pause
GOTO:EOF
:next
set "newname=%~nx1"
set "newname=%newname:XXXX=zzzz%"
echo ren %1 "%newname%
When I run the above, it replaces the XXXX's with zzzz's
When I change set "newname=%newname:XXXX=zzzz%" to set "newname=%newname:XXXX=%host%"it just deletes the X's.
What happens if you use delayed expansion?
#Echo Off
SetLocal EnableDelayedExpansion
Set "Host=%COMPUTERNAME:~4,2%"
For /F "Delims=" %%A In ('Dir /B/A-D/ON') Do (Set "NewName=%%~nA"
Echo Ren "%%~A" "!NewName:XXXX=%Host%!%%~xA"
Pause
GoTo :EOF
#echo off
FOR /f "delims=" %%G IN ('dir /a-d /b /s /o-n ^|sort /r') DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET Var=%%~nfG
SET Var=!Var: =_!
SET Var=!Var:[=_!
SET Var=!Var:]=_!
SET Var=!Var:(=_!
SET Var=!Var:)=_!
SET Var=!Var:,=_!
SET Var=!Var:'=_!
rename "%%~nfG" "!Var!"
popd
endlocal
)
Not working getting error in prompt like
_! was unexpected at this time.
Can anyone answer or correct this plz.enter code here
Also I'd compare if the name is changed before trying to rename:
:: Q:\Test\2017\08\05\SO_45521689.cmd
#echo off
FOR /f "delims=" %%G IN (
'dir /a-d /b /s /o-n ^|findstr "[,'()\[\]]"^|sort /r'
) DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET "Var=%%~nfG"
SET "Var=!Var: =_!"
SET "Var=!Var:[=_!"
SET "Var=!Var:]=_!"
SET "Var=!Var:(=_!"
SET "Var=!Var:)=_!"
SET "Var=!Var:,=_!"
SET "Var=!Var:'=_!"
if "%%~nfG" neq "!Var!" rename "%%~nfG" "!Var!"
popd
endlocal
)
SET Var=!Var:)=_!
rem ↑ this closing parenthesis closes the `... DO ()` code block.
Use
SET "Var=!Var:)=_!"
or
SET Var=!Var:^)=_!
Moreover, you cannot specify a drive or path for rename target. Use SET "Var=%%~nxG" instead of SET Var=%%~nfG.
Be aware of that your script ingurgitates all exclamation marks if a file name contains any.
The following script should do the job keeping delayed expansion disabled. Please note that operational rename command is merely ECHOed for debugging purposes.
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem choose initial directory
pushd "D:\bat\Unusual Names"
FOR /f "delims=" %%G IN ('dir /a-d /b /s /o-n') DO (
SET "_FFN=%%~fG" File Full Name
SET "Var=%%~nxG" File Name + Extension
call :renaming
)
popd
ENDLOCAL
goto :eof
:renaming
SET "Var=%Var: =_%"
SET "Var=%Var:[=_%"
SET "Var=%Var:]=_%"
SET "Var=%Var:(=_%"
SET "Var=%Var:)=_%"
SET "Var=%Var:,=_%"
SET "Var=%Var:'=_%"
ECHO rename "%_FFN%" "%Var%"
goto :eof
I did batch file which copy 3 files and need to rename it by removing last 33 characters. The copy works fine but removing last 33 characters not... I saw more then one answer on web and try it all but nothing work so far.
My batch file look like this:
for /f "delims=" %%i in ("my folder") do (
ren "%%i" "%i:~0,-33%".txt
)
I tried already:
set fName=%%i
ren "%fName%" "%fName:~0,-33%.txt"
From the information I got here, try this:
#echo off
setlocal enabledelayedexpansion
set "folderpath=[Your Folder Here...]"
cd %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
endlocal
This is similar to the answer above. You should make sure the batch file is OUTSIDE the folder.
EDIT.
When dealing with variables formed inside FOR and IF's, use delayed expansion (i.e. !var!, instead of %var%). Anyway, this is the fixed code:
#echo off
setlocal enabledelayedexpansion
::NO Last Backslash...
set "sourcepath=C:\Users\tzahi.k\Desktop\testSource\source2"
set "folderpath=C:\Users\tzahi.k\Desktop\testSource\des"
for /F "delims=" %%a in ('dir /b /od "%sourcepath%\*.txt"') do (
set "youngest=%%a"
xcopy /y "%sourcepath%\!youngest!" "%folderpath%"
)
cd /d %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
endlocal
pause
Here's the batch file you'd want to run:
#echo off
Setlocal EnableDelayedExpansion
#for /f "delims=" %%i in ('dir /b *.txt') do (
set fname=%%~ni
set fname=!fname:~0,-33!.txt
ren "%%i" "!fname!"
)
endlocal
This should work
#echo off
setlocal enabledelayedexpansion
set FOLDER_PATH=C:\Some\Path\
for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-33!%%~xf"
)
PAUSE
Or better this
#echo off & setLocal enableDELAYedeXpansion
for /f "tokens=* delims= " %%a in ('dir /b *.txt') do (
set F=%%~Na
set F=!F:~0,33!
move /y "%%a" "!F!%%~Xa"
)
I'm trying to make a loop that goes through a file with filenames on each line, set the first filename as a variable and execute the rest if the script. Then take the second line and do the same.
etc. etc.
The problem is that it only does the first line of filenames.txt
#echo off
for /F "tokens=*" %%G in (filenames.txt) do (
set filename=%%G
script
script
script
)
pause
It has be a batch file.
The whole script:
#ECHO OFF
for /F "tokens=*" %%G in (filenames.txt) do (
SET FileName=%%G
SET Word1="ts_confirmImplicitSAMM.gram"
SET Word2="SWIrcnd"
for /f "tokens=3" %%f in ('find /c /i %Word1% %FileName%') do set PairsToShow=%%f
SET /a Lines1=0, Lines2=0
FOR /f "delims=" %%a IN ('findstr "%Word1%" "%FileName%"') DO (
SET "str=%%a"
SET /a Lines1+=1
SETLOCAL enabledelayedexpansion
SET "$1!Lines1!=!str!"
FOR /f "tokens=1*delims==" %%b IN ('set "$1"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
FOR /f "delims=" %%a IN ('findstr "%Word2%" "%FileName%"') DO (
SET "str=%%a"
SET /a Lines2+=1
SETLOCAL enabledelayedexpansion
SET "$2!Lines2!=!str!"
FOR /f "tokens=1*delims==" %%b IN ('set "$2"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
SET /a Lines=Lines1+Lines2
ECHO(%Lines% lines read from %FileName%.
IF %Lines1% leq %Lines2% (SET /a MaxPairs=Lines1) ELSE SET /a MaxPairs=Lines2
IF %PairsToShow% gtr %MaxPairs% (
ECHO only text for %MaxPairs% pairs NOT %PairsToShow% :/
GOTO :END
)
(FOR /l %%a IN (1,1,%PairsToShow%) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
CALL SET "Line1=%%$1%%a%%"
CALL SET "Line2=%%$2%%a%%"
<NUL SET /p "=!Line1!"
ECHO !Line2!
ENDLOCAL
))>> result1.txt
ENDLOCAL
TYPE result1.txt| FINDSTR /V EVNT=SWIgrld >> result.txt
DEL result1.txt
PAUSE
)
Without seeing the rest of your script... you probably need to do 1 of 2 things:
Use SETLOCAL ENABLEDELAYEDEXPANSION (as 2nd line of your script) and then reference the variable filename as !filename! instead of %filename% to use the run-time value instead of the load-time value. But that could cause other problems, depending on what goes on in "script".
Just use %%G instead of filename
I have a long list of individual songs not in folders just songs, and I'd like to move them to the folder of theire artist. the songs are in the following format
artist - songname.flac
I can store them in a list, and echo it, but splitting the artist and songname in 2 vars, I can't seem to figure out.
Could someone help me with the splitting (or if you want even with the rest of the script)
this is what I have so far:
#echo off
setlocal enabledelayedexpansion
set N=0
for %%i in (*) do (
set Files[!N!]=%%~ni
set /a N+=1
)
for /l %%x in (1,1,%N%) do echo.!Files[%%x]!
pause
set SOURCE=c:\temp\test
for /f "delims=-. tokens=1,2" %%i in ('dir /b "%SOURCE%\*.flac"') do echo Artist : %%i Song : %%j
update for full script (got to check if it works with space and special chars in path) :
#echo off
setlocal enabledelayedexpansion
set SOURCE=c:\temp\test
set DESTINATION=c:\temp\test
for /f "tokens=*" %%i in ('dir /b "%SOURCE%\*.flac"') do call :OrderThatMess "%%i"
:OrderThatMess
set NAME=%1
for /f "tokens=1,2 delims=-. " %%j in (%1) do (
set ARTIST=%%j
set TITLE=%%k
if not exist "%DESTINATION%\%ARTIST%" (md "%DESTINATION%\%ARTIST%" )
copy %SOURCE%\%NAME% "%DESTINATION%\%ARTIST%\%TITLE%.flac"
)
#echo OFF &SETLOCAL ENABLEDELAYEDEXPANSION
for %%i in (*.flac) do (
set /a N+=1
FOR /f "tokens=1,2 delims=- " %%o IN ("%%~ni") DO (
set "FilesA[!N!]=%%~o"
set "FilesB[!N!]=%%~p"
)
)
for /l %%x in (1,1,%N%) do echo(!FilesA[%%x]! !filesB[%%x]!
Thanks for the answers ;) Thanks to Kayasax I fixed it
here is the full code:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set SOURCE=C:\music\folder\with\files\
for /f "tokens=1,2 delims=-" %%i in ('dir /b "%SOURCE%\*.flac"') do (
set "folder=%%i"
IF NOT EXIST "%SOURCE%\%%i" (
mkdir "%%i"
)
move "%SOURCE%\%%i-%%j" "%SOURCE%\!folder:~0, -1!\%%i-%%j"
)
pause