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"
)
Related
Hi guys I'm new to batch and have a question for my .bat to rename files.
I looked at the following solution and tried to transfer this to my problem:
Renaming file names with a BAT file
So my .bat looks like this one:
setlocal enabledelayedexpansion
set /a count=1
set padded_count=000!count!
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
ren "%%a" !padded_count!.txt
set /a count+=1
)
And I have a file with random names for .txt data.
E.g.
abc.txt
def.txt
123.txt
456.txt
And I want to change these into:
0001.txt
0002.txt
...
But when I use my .bat its just the first .txt which changes its name.
Can you explain me why? And what should I do to get all of these.
Or is it possible to handle this problem with REN in cmd with something like "ren *.txt ___"
setlocal enabledelayedexpansion
set /a count=10001
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
ren "%%a" !count:-4!.txt
set /a count+=1
)
where !count:-4! selects the final 4 characters of count.
After your comment on the requirement, This is similar to #Magoo's answer, but I am not limiting it to 4 chars.
#echo off
setlocal enabledelayedexpansion
set count=10000
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
if "!count:~1!" == "9999" set count=100000
set /a count+=1
echo ren "%%a" !count:~1!.txt
)
In this instance, once we get to 9999 we set a new count variable so out files will continue with an additional digit.
ren "file9999.txt" 9999.txt
ren "file10000.txt" 00001.txt
ren "file10001.txt" 00002.txt
...
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
I have the following batch file:
#echo off
for /f "delims=" %%F in (
'dir /b /a-d [*]*'
) do for /f "tokens=1* delims=]" %%A in (
"%%F"
) do for /f "tokens=*" %%C in ("%%B") do ren "%%F" "%%C"
I want launch it in the root directory and have it go through all directories and subdirectories performing the actions.
I tried adding /D and /r to the 'for' lines, but it doesn't appear to be working.
Do I need add something like...
for /D /r do
under the #echo off ?
Use either dir or for to get all the files, don't mix it up.
When using dir /S for recursive enumeration, regard that full paths are output rather than pure file names only.
This should do it:
#echo off
for /f "delims=" %%F in (
'dir /s /b /a-d [*]*'
) do for /f "tokens=2* delims=]" %%B in (
"%%~nxF"
) do for /f "tokens=*" %%C in ("%%B") do ren "%%~F" "%%C"
So I just changed the following in your original code:
added /s to dir (returns full paths then);
improved second for options (you never used the first token %%A, so why extract it then?);
replaced set %%F of second for by %%~nxF to just parse the file name (type for /? for details concerning substitution modifiers such as ~n, ~x);
replaced source argument "%%F" of ren command by "%%~F" to not fall into double-double-quote problems (the ~ modifier removes potential double-quotes);
You are using "dir" for the enumeration of files, so add "/s" to the DIR command.
I might refactor what you have like this to make it easier to manage.
This also does recursion.
call :TOP .
goto :EOF
:TOP
setlocal
cd "%~f1"
for /f "delims=" %%F in ('dir /b /a-d [*]*') do call :SubRoutine "%%F"
for /D %%x in (*) do call :TOP "%%x" || (echo FAILED2 "%%x" && exit /b 2)
goto :EOF
:SubRoutine
for /f "tokens=1* delims=]" %%A in ("%~1") do call :SubRoutine2 "%~1" "%%A" "%%B"
goto :EOF
:SubRoutine2
for /f "tokens=*" %%C in ("%~3") do ren "%~1" "%%C"
goto :EOF
I have what I think should be a simple problem to solve but don't know how to achieve it as someone already helped me with the code below.
I'm running the below code to rename my files
setlocal enabledelayedexpansion
for /f %%a in ('dir *RETOUCH* /b') do (
set "name=%%a"&set "name=!name:.RETOUCH=!"
ren "%%a" "!name!"
)
I need it to work with this directory
"C:\Users\Public\Desktop\Uploads\%studiosetnumber%\%ymd%\"
Currently it only works in the same location as files.
Here is the batch code for your task with some simplifications:
#echo off
setlocal EnableDelayedExpansion
for %%a in ("C:\Users\Public\Desktop\Uploads\%studiosetnumber%\%ymd%\*RETOUCH*") do (
set "name=%%~nxa"
set "name=!name:.RETOUCH=!"
ren "%%~a" "!name!"
)
endlocal
To understand how it works, open a command prompt window, execute the following commands and read help output for each command:
for /?
set /?
ren /?
You can do it many ways:
setlocal enabledelayedexpansion
Pushd "C:\Users\Public\Desktop\Uploads\%studiosetnumber%\%ymd%\"
for /f %%a in ('dir *RETOUCH* /b') do (
set "name=%%a"&set "name=!name:.RETOUCH=!"
ren "%%a" "!name!"
)
popd
setlocal enabledelayedexpansion
cd /d "C:\Users\Public\Desktop\Uploads\%studiosetnumber%\%ymd%\"
for /f %%a in ('dir *RETOUCH* /b') do (
set "name=%%a"&set "name=!name:.RETOUCH=!"
ren "%%a" "!name!"
)
setlocal enabledelayedexpansion
set "myDir=C:\Users\Public\Desktop\Uploads\%studiosetnumber%\%ymd%\"
for /f %%a in ('dir "%myDir%" *RETOUCH* /b') do (
set "name=%%a"&set "name=!name:.RETOUCH=!"
ren "%%a" "!name!"
)
i have a lot of folders , with in then there are files with name : XXXX_transcoded.j2c
i need to remove the _transcoded.j2c from the file and update to XXXX.txt
anyone have any idead how to do it
here my code so far
for /r %%i in (*.j2c) do (
call:Set %%~ni
)
:Set
set currenttext=%*
set currenttext=%currenttext:_transcoded=%
echo %currenttext%
%%~ni.Contract.xml %%i.txt
pause
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*_transcoded.j2c" '
) DO (
SET "fullname=%%a"
SET "oldname=%%~nxa"
CALL :changename
)
GOTO :EOF
:changename
SET "newname=%oldname:_transcoded.j2c=%"
ECHO REN "%fullname%" "%newname%.txt"
GOTO :eof
This should work for you. You'd have to set your directory into sourcedir.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%F in (
'dir /s /b /a-d *_transcoded.j2c'
) do (
set "full=%%F"
set "name=%%~nxF"
setlocal enableDelayedExpansion
ren "!full!" "!name:~0,-15!.txt"
endlocal
)
This should be all you need. Test it on some sample files.
#echo off
for /r %%i in (*_transcoded.j2c) do (
for /f "delims=_" %%a in ("%%~nxi") do ren "%%i" "%%a.txt"
)