I am trying to loop through every folder under "source" and remove the version extension that I've added.
For example,
\pathTo\source\FirstComponent.1.5\...
\pathTo\source\SecondComponent.4.6\...
Changed to,
\pathTo\source\FirstComponent\...
Here's what I have so far...
SETLOCAL
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%") DO FOR /f "delims=." %%j IN ("%%i") DO REN "%%~i" "%%~j%%~xi"
foxidrive gave already the right help to find the solution.
#echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "delims=." %%j IN ("%%i") DO REN "%%~i" "%%~nj"
set SourceDir=
%%~nj instead of %%j is necessary to remove the path to the folder and get only new name of folder without path.
And also possible is following as suggested by Andriy M:
#echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "delims=." %%j IN ("%%~ni") DO REN "%%~i" "%%j"
set SourceDir=
That is even better in case of one of the parent folders contains also a dot in name.
But if all folders have a dot in name like Component.One before the version string which must be kept, the following batch code can be used:
#echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "tokens=1,2* delims=." %%j IN ("%%~ni") DO REN "%%~i" "%%j.%%k"
set SourceDir=
And one more solution which removes the last 4 characters from folder name if last but third character is a dot:
#echo off
setlocal EnableDelayedExpansion
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO (
set "FolderName=%%~nxi"
if "!FolderName:~-4,1!"=="." REN "%%~i" "!FolderName:~0,-4!"
)
endlocal
If the folder names that you need to fix are always in this format
name.number.number
and you need to remove the .number.number part, you can also use this method:
SETLOCAL
SET "SourceDir=C:\pathTo\source"
FOR /D %%I IN ("%SourceDir%") DO (
FOR %%J IN ("%%~nI") DO RENAME "%%~I" "%%~nJ"
)
Basically, the idea is to treat the version numbers as name extensions. The inner loop uses the %%~nI expression to take just the folder name without the extension, i.e. without the last .number, and the result is assigned to %%J. In a similar fashion, the RENAME command uses %%~nJ to get rid of the remaining .number (the first one in the original name). And so, using the FirstComponent.1.5 name as an example, the RENAME command essentially ends up like this:
RENAME "...\FirstComponent.1.5" "FirstComponent"
Since the method essentially just drops the last two extensions, you can have however big version numbers: 10.123, 2014.12... The only limitation to the method is that version numbers must always consist of exactly two parts. (It could additionally support single-part version numbers if your names were not allowed to have a ..)
Related
I have created a batch file which should do several things, including appending all text documents of a certain format into one text file provided there are more than one text files of this format in the directory. This section of the code is below:
:multiple
SET /a count=0
ECHO.> "%location%\UAV_camera_coords_all.txt"
FOR /r "%location%\Output" %%G in ("UAV_camera_coords_*.txt") do set /a count+=1
IF %count% GTR 1 (
FOR /r "%location%\Output" %%G in ("*.txt") DO (
SET file=%%~G
TYPE "%file%">>"%location%\UAV_camera_coords_all"
)
GOTO :end
It seems that the code is crashing upon reaching the if statement even though the count variable is greater than one. None of the code in the if statement is executed and indeed none of the code which should come after the if statement is executed either. Is there any syntax or other error which may be causing this?
Besides my comment. I assume that you are not really planning on typing the output of *.txt but instead only UAV_camera_coords_*.txt.. if not, feel free to change it back to *.txt
#echo off
for /f "tokens=1,*" %%i in ('dir /s "%location%\UAV_camera_coords_*.txt" ^| findstr "File(s)"') do set cnt=%%i
if %cnt% gtr 1 (
for /f %%a in ('dir /b /s "%location%\UAV_camera_coords_*.txt"') do type "%%~a"
)>"%location%\UAV_camera_coords-all.txt"
Note, I changed the output filename to be -all and not _all as that would type the file onto itself if it was to be a txt file as well.
Edit adding back original answer, before I realised you were recursively searching through directories:
#echo off
for /f "tokens=1,*" %%i in ('dir "UAV_camera_coords_*.txt" ^| findstr "File(s)"') do if %%i gtr 1 type "UAV_camera_coords_*.txt">>"%location%\UAV_camera_coords_all"
Just for the purpose of providing an alternative, this one uses xcopy to check the file count, and copy to merge them.
PushD "%location%\Output" 2>NUL && For /F %%G In ('""%__AppDir__%xcopy.exe" "UAV_camera_coords_*.txt" . /SQL"')Do If %%G Gtr 1 Copy /Y /B "UAV_camera_coords_*.txt" "..\UAV_camera_coords_all.txt">NUL & PopD
I have several folders withing the same directory that are named like that :
001_Trial1
002_Trial2
003_Trial3
Trial4
Trial5
004_Trial6
005_Trial7
etc ...
I want to rename the folders in order to get
Trial1
Trial2
Trial3
Trial4
Trial5
Trial6
etc...
I tried to truncate it but the problem is that it will also delete the four first characters when the folder is "Trial3" it will rename it "3".
SetLocal DisableDelayedExpansion
For /D %%A In ("%1\*") Do (
Set "_d=%%~nxA"
SetLocal EnableDelayedExpansion
If Not Exist "%%~dpA!_d:~4!" Ren "%%A" "!_d:~4!"
EndLocal
)
I want all the outputs to start with "Trial"
The first thing I noticed looking at your code is, that you need to replace %1 with %~1 to get rid of the quotes.
I would then use a for /f to cut off the first part of your folder-names, so you don't need delayedExpansion and the code can handle folder-names with more or less digits at the beginning (and of course does not rename folders that already have the wanted name-format).
for /D %%D in ("%~1\*") do (
for /F "tokens=2 delims=_" %%N in ("%%~nD") do (
if not exist "%%~dpD%%~N%%~xD" ren "%%~fD" "%%~N%%~xD"
)
)
I have some of log files formatted like this "name.log"
I would like to copy those from one folder to another folder like
xcopy /y "C:\Folder1" "D:\Folder2"
And i need to rename file with created date of original file (no copy file) so that the text file in Folder2 would be like "name yyyymmddhhmm.log" if some file has the same name (date of creation) it will be overwritten.
The code:
set Source=C:\Users\user1\Desktop\Folder1
set Dest=D:\Folder2
if not exist %Dest% md %Dest%
for /F %%a in ('dir /b "%Source%\*.txt"') do call :Sub %%a
goto :eof
:Sub
set "filename=%1"
for /F %%s in ("%Source%\%1") do if %%~zs==0 goto :eof
set "datepart="
FOR /F "tokens=1-5 delims=/-: " %%a IN ('dir /tc "%filename%" ^| findstr "%filename%"') DO (
IF "%%c" neq "" SET "datepart=%%c%%a%%b%%d%%e"
)
FOR /F %%a IN ("%filename%") DO (
set "NewName=%%~na %datepart%%%~xa"
)
xcopy /y "%Source%\%filename%" "%Dest%\%NewName%*"
GOTO :EOF
The problem is that If I don't put the .bat in the same folder that origin files (Folder1),the files aren't change name. For example, if it is out, the files change name with old name and one white space.
The command windows tell me that it doesn't find the file when it get the creation date.
If I put the script into folder1 it works well.
On the other hand, if I execute the script with "Task Scheduler" I have the same problem. The files are copied but without date of creation.
What do I need to solve this problem?
#ECHO OFF
SETLOCAL
set Source=C:\Users\user1\Desktop\Folder1
set Dest=D:\Folder2
set "Source=u:\sourcedir\t w o"
set "Dest=u:\destdir"
if not exist "%Dest%" md "%Dest%"
for /F "delims=" %%k in ('dir /b "%Source%\*.log"') do call :Sub "%%k"
goto :eof
:Sub
SET "newname=%~1"
for /F "delims=" %%s in ("%Source%\%~1") do (if %%~zs==0 goto :eof
FOR /F "tokens=1-5 delims=/-: " %%a IN ('dir /tc "%Source%\%~1" ^| findstr "%~1"') DO (
IF "%%c" neq "" SET "newname=%%~ns %%c%%a%%b%%d%%e%%~xs"
)
)
ECHO(xcopy /y "%Source%\%~1" "%Dest%\%NewName%"
GOTO :EOF
The required XCOPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(XCOPY to XCOPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)
This may seem quite a radical change, but actually it really isn't.
First issue is that I overrode your directory settings with directories to suit my system. The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".
Using quotes in the md command makes the procedure immune to "paths containing separators" - I test using spaces in paths and filenames because that appears to be a popular thing to do.
I changed the directory-scan metavariable from %%a to %%k to avoid confusion with the %%a in the subroutine. Your text says that you are starting with &.log files, but your filemask was *.txt so I changed it to *.log. Quoting the parameter delivered to :Sub means the procedure will receive the entire name of the file if it contains spaces.
Within the subroutine, it would appear that yowant no name-change if the %%c part from the dir/tc scan is empty. %~1 is the supplied filename minus the quotes.
The outer loop in %%s : I added delims= to cater for spaces in filenames and used %~1 in preference to %filename%
Within the %%s block, %%s refers to the file, so you can use %%s and its modified forms like %%~zs to refer to that file's characteristics - which unfortunately do not include create-date (%%~ts contains the last-update date - you may be able to use that in te following line rather than dir and findstr)
Then as #aschipfi suggested, include the source directory in the dir otherwise the dir takes place on the current directory.
FOR /F "tokens=1-5 delims=/-: " %%a IN ("%%~ts") DO (
should work for you if you can use last-update-date in place of create-date.
So - if %%c is not empty, set the new name to (the name part of the file in %%s)+space+the date string+(the extension in %%s)
And then do the xcopy - using the old name unless it was modified.
I have some of log files formatted like this "name.log"
I would like to copy those from one folder to another folder like
xcopy /y "C:\Folder1" "D:\Folder2"
Adding I need to rename file with created date of original file (no copy file) so that the text file in Folder2 would be like "yyyymmddhhmm.log" if some file has the same name (date of creation) it will be overwritten.
I have a code with help of #Wes Larson but there is something wrong.
set Source=C:\Users\user1\Desktop\1
set Dest=C:\Users\user1\Desktop\2
if not exist %Dest% md %Dest%
for /F %%a in ('dir /b "%Source%\*.txt"') do call :Sub %%a
goto :eof
:Sub
set "filename=%1"
for /F %%s in ("%Source%\%1") do if %%~zs==0 goto :eof
set "datepart="
FOR /F "tokens=1-5 delims=/-: " %%a IN ('dir /tc "%filename%" ^| findstr "%filename%"') DO (
IF "%%c" neq "" SET "datepart=%%c%%a%%b%%d%%e"
)
FOR /F %%a IN ("%filename%") DO (
set "NewName=%%~na %datepart%%%~xa"
)
xcopy /y "%Source%\%filename%" "%Dest%\%NewName%*"
GOTO :EOF
The problem is that If I don't put the .bat in the same folder that origin files (Folder1),some files aren't change name. For example, if it is out some files change name with old name and one white space. The command windows tell me that it doesn't find the file when it get the creation date. It's very strange because some files are copied well.
What do I need to solve this problem?
Before we get to an answer: When troubleshooting/debugging batch scripts, don't use #echo off. It's great once you have it working the way you want, but comment it out when you need to see what your code is doing line by line. Also, you'll want to open a cmd window and run your script from a command line, so the window doesn't close as soon as your script finishes.
Now on to your code. This for loop is part of your problem:
FOR /f "tokens=1-3delims=/-:" %%a IN ('dir /tc "%filename%"') DO IF "%%c" neq "" SET "datepart=%%a-%%b-%%c"
Firstly, you haven't set %filename%, so this loop will fail. You should probably have a line in your :Sub like this:
set "filename=%1"
Now, assuming that %filename% is fixed, by the time this loop has finished, your %%a, %%b, and %%c variables have been set to the values in the last line of output from the command 'dir /tc "%filename%"', which is something like X Dir(s) XXX,XXX,XXX,XXX bytes free, and isn't the information you're looking for.
So, instead, you can tweak it a little so that you pipe the output of dir to findstr, looking for just the one, single line that you want to use, like this:
FOR /f "tokens=1-3delims=/-:" %%a IN ('dir /tc "%filename%" ^| findstr "%filename%"') DO (
IF "%%c" neq "" SET "datepart=%%a-%%b-%%c"
)
But then, you have another problem: your %datepart% looks like MM-DD-YYYY hh. This is because you changed your delims from the default (which includes spaces and tabs), to only the specified characters. Also, from your question, you want to also include hour and minute all formatted as "yyyymmddhhmm.log" , which means you'll need those next two tokens, too. Then your line becomes this:
FOR /f "tokens=1-5 delims=/-: " %%a IN ('dir /tc "%filename%" ^| findstr "%filename%"') DO (
IF "%%c" neq "" SET "datepart=%%c%%a%%b%%d%%e"
)
Next, your nested for loop is confusing, unnecessary, and causing problems:
FOR /f %%a IN ("%filename%") DO FOR /f %%d IN ("%datepart%") DO (
set NewName="%%~na %%d%%~xa
xcopy e/d/y "%Source%\%OrgName%" "%Dest%\%NewName%"
)
You already have the %datepart% variable, and you don't need to turn it into one of the for %%d variables to be able to reference it. So it becomes much simpler when you do it like this:
FOR /f %%a IN ("%filename%") DO (set "NewName=%%~na %datepart%%%~xa")
Also, because you run into delayed expansion issues, it's simpler to take the xcopy command out of the loop.
And once again, you have a variable %OrgName% that you never set. Fortunately, this is actually unnecessary as you already have the original file name captured as %filename%. So your overly complicated nested for loop becomes this:
FOR /f %%a IN ("%filename%") DO (
set "NewName=%%~na %datepart%%%~xa"
)
echo xcopy e/d/y "%Source%\%filename%" "%Dest%\%NewName%"
I am trying to rename files after the user inputs a string they want to remove from the file name. This works fine except when I want to rename files that are in a different location than the script:
Here is what i have so far which works if I dont specific the file path (e.g. remove C:\DATABASE\*.* /s)
SET /P X=Type in the String that you want to remove and then press ENTER:
set deletestring=%X%
for /f "delims==" %%F in ('dir C:\DATABASE\*.* /s /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)
Thanks!
Use this instead. e.g.:
remove *.*
or
remove "relative path\*.*"
or
remove C:\DATABASE\*.*
or
remove "C:\My Database\2010-*.bak"
Meaning that a directory and file mask must be specified. Here's the remove.bat file:
#echo off
setlocal enabledelayedexpansion
set mask=%~1
set mask=!mask:%~dp1=!
if not exist "%~1" (
echo No files found
goto :eof
)
pushd "%~dp1"
SET /P X=Type in the String that you want to remove and then press ENTER:
set deletestring=%X%
for /f "delims==" %%F in ('dir "%mask%" /s /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)
Your primary problem you are running into is that the 1st argument to REN can accept full path info, but the 2nd can only contain the new name without path info. You can use the ~nx modifier to extract the name and extension from the full path reported by the FOR /F command.
Your FOR /F options are not reliable - it will break if the file name contains =. You want to set delims to nothing instead.
This problem is actually more complicated than it first looks. Your code will attempt to rename both files and directories. If you want to rename the directories then you must rename in reverse alpha order because the entire list is built before any thing is renamed. If you process in normal alpha order and rename a directory, then subsequent entries within that directory will not be found.
The FIND filter in the IN() clause is not necessary. Ideally your filter should only match the file or directory name, not the path. That is doable, but a bit tricky. I would simply skip the filtering in the IN() clause and do it in the DO clause.
A file or directory name can contain ! character. But the FOR variable expansion will be corrupted if it contains ! and delayed expansion is enabled. The problem can be avoided by toggling delayed expansion on and off within the loop.
It is possible for the entire name to be removed by the search and replace, but you cannot rename a file to nothing. So I added a test to ensure there is a name left.
setlocal disableDelayedExpansion
SET /P "X=Type in the String that you want to remove and then press ENTER:"
for /f "delims=" %%F in ('dir C:\DATABASE\* /s /b ^| sort /r') do (
set "old=%%F"
set "file=%%~nxF"
setlocal enableDelayedExpansion
set "new=!file:%X%=!"
if defined new if !new! neq !file! ren "!old!" "!new!"
endlocal
)
If you don't really want to rename directories then you need to add the /A-D option. I first thought you could use a FOR /R statement, but that could potentially cause the same file to be renamed twice. FOR /F buffers the entire result set before processing any files, but FOR /R does not.
setlocal disableDelayedExpansion
SET /P "X=Type in the String that you want to remove and then press ENTER:"
for /f "delims=" %%F in ('dir C:\DATABASE\* /s /b /a-d') do (
set "old=%%F"
set "file=%%~nxF"
setlocal enableDelayedExpansion
set "new=!file:%X%=!"
if defined new if !new! neq !file! ren "!old!" "!new!"
endlocal
)