I've been trying to get the syntax right, but I'm having issues. Not only am I not getting the syntax right, but I would like this script to:
Locate all files of a certain criteria on All Drives in a network
check to see if the file is the updated file (or new file)
if it is NOT the new or updated file, locate the new file and replace it
ALSO! If I can get this to work on a schedule, such as every 6 hours... that would be a real help
I got this code to work once, but I changed it a couple times and saved over it.
#echo off
SETLOCAL
cls
:locate_old
for /d /r Z:\ %%a in (*) do if exist "%%~fa\old.file" set "oldFile=%%~fa\old.file"
if not defined oldFile (
echo old file not found...
) else (
echo old file found&GOTO oldFileCheck
)
:oldFileCheck
find "old file text" "%oldFile%" && echo old file is already updated || GOTO findNewFile
:findNewFile
for /d /r Z:\ %%a in (*) do if exist "%%~fa\new.file" set "newFile=%%~fa\new.file"
if not defined newFile (
echo no new file detected...
) else (
echo new file located...&GOTO fileSwap
)
:fileSwap
copy /y "%newFile%" "%oldFile%" && echo file updated || file not updated
If I understand your requirement, you want to replace all "old.file" files on Z:\ with "new.file" files if they aren't updated already. This is untested:
#if not defined debug_batch_files echo off
REM You can set debug_batch_files to 1 and quickly see verbose execution
pushd Z:\
for /f "delims=" %%a in ('dir /b /s /a-d new.file') do echo xcopy /D /Y "%%~fa" "%%~dpaold.file"
REM Remove "echo" from the above line if it displays the right paths.
popd
Related
I am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.
The jpeg files per content is labled as followed:
6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120
The file name is different per content
a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.
They will all be extracted to a singe folder.
So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.
For example:
Create folder named 6c82c0239f6eb839 and
move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.
I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.
#echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
set Folder=!Name:~0,3!
xcopy /y "%%a" !Folder!\
)
Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?
Thank you
#echo off
setlocal
#rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (
rem Get filename as token before the dash.
for /f "delims=-" %%B in ("%%~A") do (
rem Make dir if needed.
if not exist "%%~B" md "%%~B"
rem Check if isdir.
2>nul pushd "%%~B" && popd
if errorlevel 1 (
>&2 echo Failed isdir "%%~B".
) else (
rem Do the move operation.
>nul move /y "%%~A" "%%~B"
if errorlevel 1 (
>&2 echo Failed move "%%~A" to "%%~B"
)
)
)
)
exit /b %errorlevel%
The code is well remarked so if you want to understand
the evaluated code by changing #echo off to #echo on.
The use of %errorlevel% after the exit /b is not
required though will let you know what the errorlevel is
when #echo on is used.
The pushd tests for a directory
(even if it is a symlink).
errorlevel is checked to decide if to echo a
error message or do the move.
As the for loop variables are used direct, use of
enabledelayedexpansion is not needed.
Many commands support the argument of /? to get help
about the command. i.e. move /?.
If you only try to copy the correct jpeg to the correct folder, you can do this:
#echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
REM I presume all the files have 16 characters before the dash
set Folder=!Name:~0,16!
IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy /y "%%a" !Folder!\
)
I was not able to test.
First of all, I would like to apologize for my manners regarding my initial post.
The answer by micheal_heath has resolved my issue.
Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.
Batch file to make folders with part of file name and then copy files
#echo off
setlocal enabledelayedexpansion
for %%A in (*.psd *.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
I just changed the the following line remove the hypen and numbers to create folders for the file name properly.
for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D
I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.
I'm very new to coding.. I created a command that goes through the text file vollstaendigkeit.txt which contains a list of file names (in this case pictures), eg.
gruwo_1_1_frau.jpg
gruwo_1_1_hallo.jpg
gruwo_1_1_heissen.jpg
...
and checks whether they exist in the same folder and - if not - registers them in another file called missingfiles.txt.
If all the files are in the same directory and NOT zipped, it works fine with this code:
FOR /F %%f IN (vollstaendigkeit.txt) DO (IF EXIST %%f (ECHO %%f exists) ELSE (ECHO %%f doesn't exist >> C:\Users\Rebecca\Desktop\test\missingfiles.txt ))
But what I'd like to do is zip the pictures first and THEN run the .bat to check whether they are complete...
I tried:
FOR /F %%f IN (vollstaendigkeit.txt) DO gruwo_pictures.zip list -flat yes|find "%%f" && (IF EXIST %%f (ECHO %%f exists) ELSE (ECHO %%f doesn't exist >> C:\Users\Rebecca\Desktop\test\missingfiles.txt ))
but it doesn't work... can anyone help? I'd like to avoid unzipping but if it's not possible I wouldn't mind either.
Thanks a lot in advance!!
Best,
rebwal
It is not clear to me which archiving tool you are using. The code below uses 7-Zip to archive the files mentioned in vollstaendigkeit.txt, then reports those that do not exist into missingfiles.txt.
SET "FILELIST=vollstaendigkeit.txt"
7z a gruwo_pictures.zip -i #"%FILELIST%"
FOR /F "usebackq tokens=*" %%f IN (`TYPE "%FILELIST%"`) DO (
IF NOT EXIST "%%~f" (
ECHO "%%~f" doesn't exist >> C:\Users\Rebecca\Desktop\test\missingfiles.txt
)
)
I am trying to write a script to replace old files content with new files content which is appearing in the following format:
Old file : something.txt
New file : something.txt.new
Old file need to replaced with New file contents
New File name to be rename without new
Old file need to be deleted
Below script is not working :Could you please rewrite:
#echo off
setlocal enabledelayedexpansion
set FOLDER_PATH=D:\test
for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 (
set basename=test
ren *.txt.new !basename!.txt
)
PAUSE
i have many files in folder and need to iterate through each file,only i need is basename variable need to be filled with the name of the old file name in each iteration
#echo off
setlocal
set FOLDER_PATH=D:\test
for %%f in (%FOLDER_PATH%\*.new) do if "%%~ff" neq "%~f0" (
ECHO move /Y "%%~ff" "%%~dpnf"
)
PAUSE
or
#echo off
setlocal
set FOLDER_PATH=D:\test
for %%f in (%FOLDER_PATH%\*.new) do if "%%~ff" neq "%~f0" (
if exist "%%~dpnf" ECHO del "%%~dpnf"
ECHO rename "%%~ff" "%%~nf"
)
PAUSE
Note that operational move (del and rename) commands are merely ECHOed for debugging purposes.
Also note that if "%%~ff" neq "%~f0" could be omitted iterating *.new files as %~f0 has an extension of .bat or .cmd (try echo %~x0 %~f0).
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~ff etc. special page) Command Line arguments (Parameters)
(>, 2>&1 etc. special page) Redirection
You missed a \in %FOLDER_PATH%\*.
You want to rename *.new files only, so why not using this pattern rather than *? If you are interested in files matching the pattern *.txt.new, then specify this instead of *.
There is no need for variable basename; simply use %%~nf to remove the (last) suffix (.new).
The ren command throws an error in case the target file already exists; since you want it to be overwritten, use move /Y instead. Add > nul to hide the summary 1 file(s) moved..
Remove the if query after having resolved item 2.
All in all, your script can be reduced to:
#echo off
set "FOLDER_PATH=D:\test"
rem // Ensure to specify the correct pattern, `*.new` or `*.txt.new`:
for %%f in ("%FOLDER_PATH%\*.txt.new") do (
> nul move /Y "%%~f" "%%~nf"
)
pause
Invalid question Invalid answer
source 1
#echo off
set FOLDER_PATH=D:\test
cd /d "%FOLDER_PATH%"
ren "*.txt.new" "*.txt"
source 2
#echo off
set FOLDER_PATH=D:\test
cd /d "%FOLDER_PATH%"
ren "*.txt" "*.txt.new"
Thanks for any help in advance.
I am trying to make a batch file to move files in some folder.
Just a bit background.
I have a folder and the structure as below:
c:\test
app1\app1.exe
app2\app2.exe
app3\app3.doc
xx[xx].exe
xx2\xx2.exe
xx3\xx3.EXE
In other words, I have a folder that contains a few sub folders, each of the sub-dir has some files. What I want to do is using a script, loop all the folders, move all files contain "app" to folder app, then move all files have "xx" to folder xx - in order to minimize the effort, I've already created these two folders so the script doesn't need to decide if it has to make a new dir.
Below is my script,
#echo off
rem loop xx
FOR /r "C:\test\" %%G in (*xx*.*) DO (
Echo Found file - %%G
copy %%G c:\testf\xx\
set pathname=%%G
for %%K in ("%pathname%") do ( set filepath=%%~dpK
set filename=%%~nxK
echo Filepath is %filepath%
echo %filename% >> c:\output.log )
echo full is %pathname% )
rem loop app
FOR /r "C:\test\" %%H in (*app*.*) DO (
Echo Found file - %%H
copy %%H c:\testf\app\
set pathname=%%H
for %%L in ("%pathname%") do ( set filepath=%%~dpL
set filename=%%~nxL
echo Filepath is %filepath%
echo %filename% >> c:\output.log )
echo full is %pathname% )
Echo "All Done"
The current output log shows
xx3.EXE
xx3.EXE
xx3.EXE
app3.EXE
app3.EXE
app3.EXE
and xx files were sent to xx folder only but app files didnt.
Could I have any help to deal with this problem?
I really appreciate any help you can provide.
Perhaps the path of your app files includes spaces - that would break the COPY command unless you include quotes.
Your script is much more complicated than need be. There isn't any need for the inner loop. You can also eliminate code by using a loop to get your two strings. I would have written it like so to get the same result, with the COPY command fixed with quotes:
#echo off
for %%A in (xx app) do (
for /r "c:\test\" %%F in (*%%A*.*) do (
echo Found file - %%F
copy "%%F" "c:\testf\%%A\"
echo Filepath is %%~dpF
echo %%~nxF >>c:\output.log
echo full is %%F
)
)
echo "All Done"
Please note that copying the same file name from multiple source directories into one target directory will result in only one file - last one copied wins.
You can use FIND to test your files :
#echo off&cls
for %%a in (xx app) do call:Treat %%a
exit /b
:Treat
echo Treating : [%1]
for /f "delims=" %%a in ('dir/a-d/b/s ^| find "%1"') do echo copy "%%a" "c:\test\%1"
I put an echo before the copy that you can test the output.
I have several folders that have files with double file extensions along with regular file extensions. I need to create a batch script to search all the folders and remove the last extension with any files that have double extensions. None of the file extensions are consistent.
Here's an example
C:\test\regular.exe
C:\test\picture.jpg.doc
C:\newtest\document.doc.pdf
End Result I need
C:\test\regular.exe
C:\test\picture.jpg
C:\newtest\document.doc
#ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF NOT EXIST "%%~dpni" ECHO REN "%%~fi" "%%~ni"
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" ECHO CAN NOT REN "%%~fi" "%%~ni"
)
GOTO :EOF
This batch should accomplish the task.
For each file in the tree rooted at sourcedir, if the NAME of the file itself contains an 'extension' and the filename without the original extension does not exist, then rename the file. That way, if ...picture.jpg.doc is found, the rename should occur only if ...picture.jpg does not exist.
The command to rename is simply ECHOed. You'd need to remove the ECHO keyword to activate the rename - after verifying that's what you want to do.
I've added a second line to report that a rename could not be done because of an existing file.. This could be done very slightly better, but it will work.
Revised to modify name in case simple rename can not be done.
Caution - this version will rename immediately - there are no ECHOes to provide a list first because it's nonsense to provide such a list when renaming a file may produce different results on the main rename run.
#ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" (
SET renreq=Y
FOR %%a IN (new alt extra another 1 2 3 4 5 6 7 8 9) DO IF DEFINED renreq (
IF NOT EXIST "%%~dpi%%~nn_%%a%%~xn" (
REN "%%~fi" "%%~nn_%%a%%~xn"
SET "renreq="
)
)
IF DEFINED renreq ECHO CAN NOT REN "%%~fi"
) ELSE (
REN "%%~fi" "%%~ni"
)
)
GOTO :EOF
Reasonably obviously, the list of "extras" can be extended if required.
Try this and remove the echo, if the output is OK:
#echo off &setlocal
for /r \ %%i in (*) do (
for %%j in ("%%~ni") do if "%%~xj" neq "" echo ren "%%~fi" "%%~nj"
)
Edit: added support for the entire HD.