I have the following code:
#echo off
setlocal disableDelayedExpansion
set "src=C:\test"
set "dst=C:\test2"
set "search=test"
for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal enableDelayedExpansion
copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!"
endlocal
)
what this code does is copy a file from 1 location and put it on other location and change the name and put date/time in the filename.
Where i was looking for is that behind the filename and time also comes the location of the copyed file. so something like this:
Filename-10-03-2014-15:58:45-C:\test\test1\testfile.txt
so i can see the date time and old path in my filename.
Hope you guys can help.
Kind regards,
Kaluh
#echo off
setlocal disableDelayedExpansion
set "src=C:\sourcedir"
set "dst=C:\destdir"
set "search=jpg"
for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal ENABLEDELAYEDEXPANSION
SET "appendix=!full::=-!"
SET "appendix=!appendix:\=_!"
ECHO copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!-!appendix!"
endlocal
)
GOTO :EOF
I changed the search pattern, source and destination directories to suit my system.
You would need to nominate your own substitute characters for : and \ which cannot appear in a filename.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.
Related
There are two folders holding equal amounts of files between them.
I'd like to apply the names from one set ; to the other set of files in no particular order. Inherit the name, but retain the extension .
Input files are .bmp Output files are .ini ( has a gear symbol ).
Example :
folder 1- Flowers.bmp ; folder 2- blank.ini
. To this:
folder 1- Flowers.bmp ; folder 2- Flowers.ini
There would be more files , but equal .
The .ini files are all copies . So they may have a generic name and numbered if that matters to know . Those would all receive one name each from the other .bmp files in the other folder.
Normally I have both folders situated on the Desktop .
I make sure both folders have equal number of files between them . That would be a constant .
I'm trying to stream line some menial repetitive daily tasks .
I did search and what I have found does not really help.
#ECHO OFF
SET "vers=%~1"
IF "%vers%" == "" SET /P "vers=Enter Vers: "
FOR %%F IN (file_XX_*.*) DO CALL :process "%%F"
GOTO :EOF
:process
SET "name=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "name=!name:_XX_=_%vers%_!"
RENAME %1 "%name%"
ENDLOCAL
Hoping to find a solution to this finally .
Ok, here is a slightly long version, but it makes sure it gets the content of each folder.
Note You must ensure that the path to both folders are correctly specified in the third and fourth line:
#echo off
setlocal enabledelayedexpansion
set "source=%userprofile%\Desktop\folder 1"
set "destination=%userprofile%\Desktop\folder 2"
set /a num=0
for %%a in ("%source%\*") do (
set /a num+=1
set "fr!num!m=%%~na"
)
set /a oldn=!num!
set /a num=0
for %%b in ("%destination%\*") do (
set /a num+=1
set "to!num!r!=%%~nxb"
set "ext=%%~xb"
)
pushd "%destination%"
for /l %%i in (1,1,!oldn!) do ren "!to%%ir!" "!fr%%im!%ext%"
popd
pause
You can remove pause at the bottom of the script, once you are happy with the output.
This is not the most effective way, but considering your limited batch experience, I guess an understandable approach is better than an optimized one (using array-like variables).
#echo off
setlocal enabledelayedexpansion
(for %%A in ("folder 1\*.bmp") do echo %%~nA)> "%temp%\names.txt"
<"%temp%\names.txt" (
for %%A in ("folder 2\*.ini") do (
set /p "name="
ECHO ren "%%A" "!name!.ini"
))
The first for loop creates a list of the desired names (from Folder 1). The modifier %%~nA returns the name only.
The second for processes each file in Folder 2 and takes a name from the previously generated file for each file. This depends on the fact that the number of files in both folders are the same, else you may get undesired results.
I disabled the actual ren command by just echoing it. Check the output and only when you are satisfied, remove the ECHO.
I need a batch file that renames my files in according to the folder name.
For example I have this folder:
E:\PROGET\01_Progetti\1_TRATTATIVE\IT.16.9291_Fabbricato ad Milano (MI) - Ing. Bianchi\03-CALCOLO\02-XLS\
Which contains
CB-Tech_92XX - .xls
Punz_92XX - .xls
I want to rename them to
CB-Tech_9291 - .xls
Punz_9291 - .xls
Is it possible?
EDIT:
I've found this code from a guy who asked for code and didn't get any complain Rename all files in a directory with a Windows batch script
I've changed it a little bit:
#echo off
setlocal enableDelayedExpansion
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=9XXX!"
)
#pause
Now I just have to understand how to get the path (done), extract only the numbers (not yet) and store in a variable (done).
To set a variable should be something like that
set "number=projectnumber"
SET mypath=%~dp0
ok now I've the path, need to extract only 4 characters after the IT.16.
Will edit later :)
EDIT 2:
#echo off
setlocal enableDelayedExpansion
SET mypath=%~dp0
set projectnumber=%mypath:~41,4%
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=%projectnumber:~0%!"
)
#pause
YEAH! This works for this specific folder!!
Now I just need to understand how to search and extract the number inside the path to make it more general.
I'm looking for a function that returns the position of the first char of the string IT.16.
Any advice?
This is the complete solution of this question:
#echo off
setlocal enableDelayedExpansion
SET mypath=%~dp0
set "projectnumber=%mypath:*IT.16.=%"
set "projectnumber=%projectnumber:~,4%"
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=%projectnumber:~0%!"
)
Recently I started working and my first task is to write a batch file that automatically changes filenames to filename_date with the original file-ending.
For that you should be able to write paths into a textfile (e.g. paths.txt) and when you start the program, it should take any line (=path->file) from there and rename it.
I got it to work on my PC quiet well but as I gave it to testing they asked to make the use of wildcards Z:\Path\*.* possible.
My current code looks as follows:
#echo off
setlocal EnableDelayedExpansion
cd %~dp0
For /F "tokens=*" %%m in (paths.txt) do (
set path=%%~dpm
set name=%%~nxm
pushd "!path!"
dir
For /r !path! %%f in (!name!) do (
set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
set "name=!name!_"
set "name=!name!!date:~6,4!"
set "name=!name!!date:~3,2!"
set "name=!name!!date:~0,2!"
set "name=!name!!ending!"
copy "!datsave!" "!name!"
del "!datsave!"
cls
popd
)
)
I know that a lot of it is probably easier and more efficient to do, but this is my first batch project and I am quiet happy except for the wildcard problem.
So an example would be:
C:\Some\Path\*.*
This line would be in paths.txt.
With the splitting
set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
I get the following:
path: C:\Some\Path
name: C:\Some\Path
ending: -empty-
datsave: C:\Some\Path
because name is set to the Path at the start of the first FOR-Loop. But that seems to be working if I do not use wildcards.
Now the question: Why does this happen and how do I get rid of it? Or do I just use the wrong type of wildcards?
Again: This is my first time I work with batch, so it might be something simple ;)
Ok, I figured out 2 problems and now it works
set name=%%~nxm evaluates the wildcard. Even if name is *.txt it will return bar.txt.
I replaced that by a basename computation instead: set name=!name:*\=! done enough times (not very subtle but hey batch files forces us to do such things) which preserves the wildcard
The other problem is the for /R loop: after pushd, the argument needs to be . or it won't be scanned.
Last minor one: use rename instead of copy plus delete. It preserves file time and is very fast. Copying then deleting a large file can take a long time.
#echo off
set DEPTH=20
setlocal EnableDelayedExpansion
cd %~dp0
For /F %%m in (paths.txt) do (
set pth=%%~dpm
set z=%%m
set name=!z!
rem brutal basename. We cannot break the inner loop or
rem it would break the upper loop too
for /L %%I in (1,1,%DEPTH%) do set name=!name:*\=!
rem but we can check if it is really a basename
set chkname=!name:*\=!
if not !chkname!==!name! ( echo please increase DEPTH value
pause
exit /B)
rem set name=%%~nxm
pushd "!pth!"
For /r . %%f in (!name!) do (
set pth=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
set "name=!name!_!date:~6,4!!date:~3,2!!date:~0,2!!ending!
echo renaming "!datsave!" to "!name!"
rem ren "!datsave!" "!name!"
popd
)
)
paths.txt contains just a line C:\full\path\to\test\*.txt
my test directory contains 2 text files and 1 other file
output:
renaming "bar.txt" to "bar_20160812.txt"
renaming "foo.txt" to "foo_20160812.txt"
(just uncomment the ren line to get the job done)
Weeeeell First of all thanks again to #Jean-François Fabre and #aschipfl for their patience with me :)
After the hint with the second batch file I had to test a few things as not everything worked as fine, but now everything works great!
Code of the Main file:
#echo off
setlocal EnableDelayedExpansion
cd %~dp0
set DEPTH=20
For /F %%m in (paths.txt) do (
pause
set pth=%%~dpm
REM pushd !pth!
REM set origpth=!cd!
REM popd
set z=%%m
set name=!z!
For /L %%i in (1,1,%DEPTH%) do set
name=!name:*\=!
set chkname=!name:*\=!
if not !chkname!==!name! ( echo depth to small
pause
exit /B)
rem set name=%%~nxm
pushd "!pth!"
For /r . %%f in (!name!) do (
pushd %~dp0
call renamefiles.bat %%f REM "!origpth!"
popd
)
)
And the code of the sub-file:
#echo off
REM set pth=%~dp1
REM set origpth=%2
REM set origpth=%origpath:"=%\
REM If !pth!==%origpth% (
set path=%~dp1
set name=%~n1
set ending=%~x1
set datsave=%~nx1
pushd !path!
set "name=!name!_!date:~6,4!!date:~3,2!!date:~0,2!!ending!"
pause
echo renaming "!datsave!" to "!name!"
rem "!datsave!" "!name!"
cls
popd
REM )
EDIT: After testing around a bit I figured, that subfolders are included as well! I put extra code to both codes marked with REM and two extra spaces. Take out those REM's and the programm will not longer include subfolders when renaming :)
Need to copy text from one file to another.
file1 as below:
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
file2 will be
locked/agent.jms.remote.host=mkdfvsh_a-2341
locked/agent.jms.remote.port=1234
& need to replace when creating the second file
command i used:
SET newfile=file2
SEt filetoCOpy=file1
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:<AGENTHOST>=%AGENTHOST%!
SET line=!line:<AGENTPORT>=%AGENTPORT%!
echo !line!>>%newfile%
)
Result i got
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
variable value not changing.
Can someone help here what's wrong?
You should be getting an error with the code you posted. The < and > are treated as redirection unless they are escaped or quoted:
SET line=!line:^<AGENTHOST^>=%AGENTHOST%!
SET line=!line:^<AGENTPORT^>=%AGENTPORT%!
or
SET "line=!line:<AGENTHOST>=%AGENTHOST%!"
SET "line=!line:<AGENTPORT>=%AGENTPORT%!"
But you still have at least one problem - If you enable delayed expansion within a loop, then you must ENDLOCAL at the end of the loop, otherwise you can run out of SETLOCAL stack space.
Also, I don't see where AGENTHOST or AGENTPORT is defined.
You can try something like that :
#echo off
SET newfile=file2.txt
SEt filetoCOpy=file1.txt
set "ReplaceString1=mkdfvsh_a-2341"
set "ReplaceString2=1234"
If exist %newfile% Del %newfile%
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:^<AGENTHOST^>=%ReplaceString1%!
SET line=!line:^<AGENTPORT^>=%ReplaceString2%!
echo !line!>>%newfile%
)
EndLocal
start "" %newfile%
I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")