rename text from file ,and string batch file - batch-file

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"
)

Related

Batch - How to rename a file with information contained in it?

I need to rename .txt files with information contained in them. I can't make the ren command works. The remaining code seems correct, the 'string' is correct too.
Any help and remark is welcome. Thank you.
EDIT: the ren command isn't working but instead displaying "Filename already exists, or can't find the file".
If I replace ren "%%F" !string! by ren "%%F" "example.txt" the first .txt file of my folder will be correctly renamed.
#echo off
pause
Set "ActualFolder=D:\folder"
cd /d %ActualFolder%
Setlocal EnableDelayedExpansion
FOR %%F IN (*.txt) DO (
echo.
ECHO Previous name: %%F
FOR /F "tokens=5" %%T IN ('FINDSTR /C:"name1" %%F') DO (
SET "n1=%%T"
)
FOR /F "tokens=3" %%T IN ('FINDSTR /C:"name2" %%F') DO (
SET "n2=%%T"
)
FOR /F "tokens=5" %%T IN ('FINDSTR /C:"name3" %%F') DO (
SET "n3=%%T"
)
SET string="!n1! !n2! !n3!.txt"
echo New name: !string!
ren "%%F" !string!
)
PAUSE

batch file remove X characters of filename

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"
)

Need my renaming function in my script to work with a specific directory other than directory script is located

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!"
)

Batch file to loop through a directory and create a variable based on each file

I have a text file with a list of server IP addresses and the code below (which I've scrapped together from other coding) loops through it and brings back a modified date of a named file for each server in the list...
#ECHO On
SETLOCAL
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
CALL :getmod %%a
)
GOTO :EOF
:getmod
SET Server=%1
SET File=Abs_Client.exe
FOR %%i IN ("\\%Server%\C$\Com_Dir\%File%") DO SET modif_time=%%~ti
Echo %Server% %File% %modif_time% >> "C:\Scripts\Server_App_Mod_date.txt"
GOTO :eof
That works great...however, what I'd like to do is create another loop around it which creates a variable for each file in a directory and pass that into the code above instead of having to manually change the 'SET File' as shown above for individual files.
Something along the lines of;
#ECHO On
SETLOCAL
FOR /D %VAR IN ("\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update") DO (
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
CALL :getmod %%a
)
GOTO :EOF
:getmod
SET Server=%1
SET File=%VAR
FOR %%i IN ("\\%Server%\C$\Com_Dir\%File%") DO SET modif_time=%%~ti
Echo %Server% %File% %modif_time% >> "C:\Scripts\Server_App_Mod_date.txt"
GOTO :eof
)
Clearly it's wrong so any ideas/help please?
haven't testet, but maybe a hint in the right direction:
#ECHO ON
SETLOCAL
FOR /F "TOKENS=*" %%F IN ('DIR "\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update" /s /b /a:-d') DO (
FOR /F %%A IN (C:\Scripts\Servers.txt) DO (
CALL :getmod %%A "%%~nxF"
)
)
GOTO :EOF
:getmod
SET Server=%1
SET "tmpFile=%~2"
FOR %%I IN ("\\%Server%\C$\Com_Dir\%tmpFile%") DO ECHO %Server% %tmpFile% %%~tI >> "C:\Scripts\Server_App_Mod_date.txt"
GOTO :EOF
As far as i know, FOR /D only executes for directorys and if i understand your question, you have files in "Prod Apps\Server_Update", for each you like to have the file-date/time from the target-server... right?
Edit:
Maybe this works too:
FOR /F "TOKENS=*" %%F IN ('DIR "\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update" /s /b /a:-d') DO (
FOR /F %%A IN (C:\Scripts\Servers.txt) DO (
FOR %%X IN ("\\%%A\C$\Com_Dir\%%~nxF") DO ECHO %%A %%~nxF %%~tX >> "C:\Scripts\Server_App_Mod_date.txt"
)
)
without the :getmod
Edit: /b-switch was missing from the first DIR-Command in 2nd suggestion
#ECHO On
SETLOCAL
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
FOR /f "delims=" %%i IN ('dir /b/a-d "\\%%a\C$\Com_Dir\*"') DO Echo %%a %%i %%~ti >> "C:\Scripts\Server_App_Mod_date.txt"
)
GOTO :EOF
Should work, IIUC. Can't test, I'm afraid...
[Edit - removed call to getmod - not required]

How to get a Substring from list of file names

I want to develop the following logic
Read all files in a directory
Extract the first part of the filename – this will be the partner name
Extract anything after the first underscore- this will be filename
Eg: ZZTEST_123_abc_doc.txt  ZZTEST is partner. 123_abc_doc.txt is the filename.
Below is the code I developed
#echo off
setlocal ENABLEDELAYEDEXPANSION
Set Test_Dir=C:\Axway\projects\Cardinal\dosscript\test
cd %Test_Dir%
for /r %%a in (*.*) do (
Set "fname1=%%~nxa"
echo Filename is :!fname1!
for /f "tokens=1 delims=_" %%i in ("!fname1!") do (
Set "partner=%%i"
echo Partner is :!partner!
Set "str_tmp=!partner!_"
echo !str_tmp!
call :strlength length !str_tmp!
echo !length!
set fname=!fname1:~%length%!
echo !fname1:~%length%!
)
)
goto :eof
:strlength
setlocal enableextensions
set "#=%~2"
set length=0
:stringLengthLoop
if defined # (set "#=%#:~1%"&set /A length+=1&goto stringLengthLoop)
endlocal && set "%~1=%length%"
GOTO :EOF
But the result is
ID_ZZRoutingID_filename.txt
Filename is :ZZRoutingID_ZZRoutingID_filename1.txt
Partner is :ZZRoutingID
12
Result: ID_ZZRoutingID_filename1.txt
The result should be ZZRoutingID_filename1.txt but i am getting
ID_ZZRoutingID_filename1.txt.
Please help
The purpose of the length calculation is not clear to me, but I would suggest adding an asterisk following the 1 in your for /f "tokens=1 delims=_". You would then get the "filename" you were looking for through %%j.
I tested it like this:
#echo off
setlocal EnableDelayedExpansion
set source=D:\Program Files\Somewhere
cd %source%
for /r %%i in (*.*) do (
for /f "tokens=1* delims=_" %%j in ( "%%~nxi" ) do (
echo partner: %%j
echo name: %%k
)
)
endlocal
If you do not need to recurse through sub-directories:
#echo off
set source=D:\Program Files\Somewhere
for /f "tokens=1* delims=_" %%i in ( 'dir "%source%" /b /a-d' ) do (
echo partner: %%i
echo filename: %%j
)
dir /b /a-d retrieves the list of a directory's content except its sub-directories:
D:\Program Files\Somewhere>dir /b /a-d
ZZTEST_123_456.txt
ABCDEF_890_FFF.doc
FOOBAR_567_###.zzz

Resources