I have files in one folder with the names like
John-Smith-014_15616812_5165
Robert-Walker-014_15644812_5145
I want these files to be checked and if they matched to the already existed one then automatically moved there.
Destination folders are already created and they look like:
John-Smith-156.81.251.452
Robert-Walker-12.108.36.5418
I have tried this code:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\dest"
FOR %%G IN (*) DO (
FOR /F "tokens=1 delims=_" %%a IN ("%%G") do (
set "outFolder=%%a"
for /D %%i in (*.*) do (
for /F "tokens=1 delims=_" %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
move "%%G" "!outfolder!"
)
)
popd
pause
This code partly works but it can't make difference between Robert-Walker to Robert-Brown and it will move both of them to the folder with the name Robert-Walker-12.108.36.5418
In this code it won't check both first and second tokens in order to sort those files according to their name and surname, instead it sorts only by their name.
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
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"
)
Hello i have a batch script but i cant work out how to change the path to scan all subfolders within the directory.
In other words i dont want -
C:\Users\ally\Desktop\Documents\Table\CSV versions\2014\
but rather:
C:\Users\ally\Desktop\Documents\Table\CSV versions
as there are lots of different years of data in seperate folders.
Also to note within year folder there are month folders and within that there are the csv files.
#echo off
setlocal enabledelayedexpansion
set "target=C:\Users\ally\Desktop\Documents\All"
cd /d "C:\Users\ally\Desktop\Documents\Table\CSV versions\2014\"
for /L %%a in (101,1,148) do (
set num=%%a
del "%target%\-!num:~-2!.csv" 2>nul
>"%target%\-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "%target%\-%%d.csv.txt" echo %%b
)
)
)
ren "%target%\*.csv.txt" *.
pause
To process every folder under the All tree then you can use a for /d /r loop and pushd/popd to set the current folder.
This assumes that every folder has the files you want to process.
Test this on a copy of your data and change the folder to point to it.
You seem to be deleting .csv files, creating .csv.txt files and then trying to process *.csv in the lower loop. Should that be *.csv.txt ?
#echo off
setlocal enabledelayedexpansion
for /d /r "c:\Users\ally\Desktop\Documents\All" %%z in (*) do (
pushd "%%z"
for /L %%a in (101,1,148) do (
set num=%%a
del "-!num:~-2!.csv" 2>nul
>"-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "-%%d.csv.txt" echo %%b
)
)
)
ren "*.csv.txt" *.
popd
)
pause
Try adding another loop that will walk down the directory tree and if it finds a csv file, it will process it:
#echo off
setlocal enabledelayedexpansion
set "target=C:\Users\ally\Desktop\Documents\All"
for /f "tokens=1* delims=" %%D in ('dir /s /b /o:-n /a:d "C:\Users\ally\Desktop\Documents\Table\CSV versions"') do (
cd /d "%%~fD"
if exist *.csv (
for /L %%a in (101,1,148) do (
set num=%%a
del "%target%\-!num:~-2!.csv" 2>nul
>"%target%\-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "%target%\-%%d.csv.txt" echo %%b
)
)
)
ren "%target%\*.csv.txt" *.
)
)
pause
Explanation of the dir switches:
/s - Displays files in specified directory and all subdirectories.
/b - Uses bare format (no heading information or summary).
/o:in - Lists the files in reverse order
/a:d - Displays only folders
Explanation of the %%~fD: expands %A to a fully qualified path name (from for /?)
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"
)