In most modern systems, the system will automatically create extra files that are unnecessary. How can I create a .bat file that makes use of a checklist to delete any file that isn't on it?
This would be useful for removing automatically generated backup files, plot logs, error logs, etc. in the customization files I have created.
Try this, make sure to set SEARCHDIR to the real dir, and populate keep.txt with the files you want to keep, 1 file per line, use the full path including the dir. If you don't want to use the dir, adjust the DIR /S /B command so it outputs the same format as what is in your keep.txt
#echo off
setlocal enabledelayedexpansion
set KEEPFILES=keep.txt
set SEARCHDIR=.\test_folder
SET /a KEEPTHISFILE=2
FOR /f "tokens=*" %%A IN ( 'DIR /S /B %SEARCHDIR%\*' ) DO (
SET /a KEEPTHISFILE=0
for /f "tokens=1,* delims=ΒΆ" %%B in ( '"type %KEEPFILES%"') do (
if "%%A"=="%%B" SET /a KEEPTHISFILE=1
)
if "!KEEPTHISFILE!"=="0" (
echo "deleting %%A"
del /f %%A
)
)
Make a file called 'deletestuff.bat' (or whatever you want it to be called, as long as it is a .bat). Next, fill it with:
rm *.tmp
rm *.blah
Where .tmp, and .blah are the extensions of the files that you would like to delete (they might be .log, for instance). Test it first by copying a sample of files into a new folder and running the new .bat there, just to make sure it doesn't delete important things.
Related
I have a folder of PDF files that have a consistent naming convention. I want to create a zip file of these PDF files but named the zip file using the portion of the file that is before the # -- all of the files are the same in the front (it is the NTID of the user that created the pdf files).
As an example these are what the files might look like in the PDF output folder (there could be 100 files all that start with the same UserID before the #:
UserID#Carlos+Alberto+Mafra-+bribery-2019-05-16
UserID#MAJELA+HOSPITALAR+LTDA-+bribery-2019-05-16
(Ideally, I would also want the current date appended to the zip file)
The zip should be called UserID-2019-05-16.zip based on the example above.
This is the code I am trying to use but not having success...
I created a batch script using others suggestions for each step. but can't get it to work end to end.
FOR %%F IN ("C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\*.pdf") DO (
set filename=%%F
goto next
)
:next
echo "%filename%"
set zipfile=%filename%
for /f "tokens=1 delims=#" %%a in ("%zipfile%") do (
)
cd "C:\Program Files\7-Zip\"
7z.exe" a "C:\Users\SA-JJC-HCC_Ops\JNJ\HCC&P Alteryx - Documents\EPiC\GoogleSearches\zip\" && %zipfile% && ".zip" "C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\*.pdf"
One zip file with all the PDFs that are using the first part of the string from the file names in the PDF folder.
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR /f "tokens=1*delims=#" %%a IN (
'dir /b /a-d "%sourcedir%\*#*-????-??-??.pdf" '
) DO (
SET "pre=%%~a"
SET "post=%%~nb"
SET "post=!post:~-10!"
IF DEFINED post ECHO "C:\Program Files\7-Zip\7z" a "%destdir%\!pre!-!post!.zip" "%sourcedir%\%%~a#%%~b"
IF NOT DEFINED post ECHO SKIP "%%~a#%%~b"
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used the variablenames pre and post to ensure that the names used in this process are not keywords like the more logical date.
Read a list of all filenames matching the pattern *#*-????-??-??.pdf in the source directory, tokenising on #. Assign the userid to %%a and thence to pre and the "name" part of the dregs of the actual filename to post, then select only the last 10 characters of post using delayed-expansion.
There is an opportunity here to further process post to check whether it truly fits the pattern for a date, if that is required. That routine may return post either unmolested or empty. If it's not empty then construct the required 7z command (you may wish to add -tzip) and echo this for verification - remove the echo to actuate the 7z compression. If post is emptied by a pattern-checking routine, then the filename will simply be reported as having been skipped.
If it is indeed current date you want to append to the end of the zip file, then we need to get the non locale dependent date and time. This will then copy each of the pdf files that starts with UserID to a zip with a date of the day you run the script UserID-2019-05-17 :
#echo off
set "outDir=C:\Users\SA-JJC-HCC_Ops\JNJ\HCC&P Alteryx - Documents\EPiC\GoogleSearches\zip\"
set "inDir=C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\"
for /f "tokens=1,2 delims==" %%i in ('wmic os get LocalDateTime /VALUE') do (
if ".%%i."==".LocalDateTime." set mydate=%%j
)
set mydate=%mydate:~0,4%-%mydate:~4,2%-%mydate:~6,2%
for %%a in (*.pdf) do for /f "delims=#" %%i in ('dir /b /a-d %%a') do (
"C:\Program Files\7-Zip\7z" a "%outDir%%%i-%mydate%.zip" "%inDir%%%~a"
)
If in fact you want to append the date of the filename instead (In other words create Zip files for each file with a different date as well as matching userid):
#echo off
setlocal enabledelayedexpansion
set "outDir=C:\Users\SA-JJC-HCC_Ops\JNJ\HCC&P Alteryx - Documents\EPiC\GoogleSearches\zip\"
set "inDir=C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\"
for %%a in (*.pdf) do for /f "tokens=1,* delims=#" %%i in ('dir /b /a-d %%a') do (
set fdate=%%~nj
set fdate=!fdate:~-10!
echo "C:\Program Files\7-Zip\7z" a "%outDir%%%i-!fdate!.zip "%inDir%%%~a"
)
I have a folder called TEST. Inside there are 30 files.
Example:
DIM1_UPI_20170102.TXT
DIM2_UPI_20170908.TXT
DIM3_UPI_20180101.TXT
...
I have to rename them by removing the date tag
Exapmple:
DIM1_UPI.TXT
DIM2_UPI.TXT
DIM3_UPI.TXT
Can you please help me writing this in batch file?
Assuming your files are all starting with DIM
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "*.TXT" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-9!%%~xi
)
Once you can confirm that it does what you want, and ONLY then, remove the echofrom the last line to actually rename the files.
Important Note. If you have files with similar names, but different date entries, this will not work as you think. as Example:
DIM2_UPI_20170910.TXT
DIM2_UPI_20170908.TXT
The names are the same, but dates differ, making each filename Unique. If you rename them, there can be only 1 DIM2_UPI.TXT So as long as you understand this, you will be fine.
Edit: based on Amazon drive question. Note you need to change the directory portion to how you access amazon drive.
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "DIM*" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-16!%%~xi
)
I've been trying to tackle this problem for few days now to no avail. I have no programming experience whatsoever and this task has been driving me nuts.
I have a txt file that has a list of paths to files that need to be copied. Some 8000 paths are in this file.
Copying each item isn't such a big deal as I can just add the copy command and destination before/after each path.
The crux of my issue is that many of these files have the same filename and when they're in different directories it's not a problem.
However I need all the files in the same destination folder and it keeps overwriting itself.
To sum up, I have a .txt file that basically looks like this:
D:\Big folder\Folder\Subfolder a\filea.file
D:\Big folder\Folder3\Subfolder za\filek.file
D:\Big folder\Folder\Subfolder ds\filed.file
D:\Big folder8\Folder\Subfolder p\filea.file...
I need some tool that will let me copy all of these files into one destination folder, and make sure any duplicates get renamed so that they aren't overwritten.
such that filea.file and filea.file become filea.file and filea1.file
EDIT: so far I've come up with
FOR /F "tokens=* usebackq" %i IN (`type "C:\Users\username\Desktop\completelist.txt"`) DO COPY "%i" "E:\destination\"
which does the read and copy job but not the rename part
Save script below to Copy.bat, open Cmd Prompt from the script directory, and run the bat. It works well for me. Post exact errors, if any.
#echo off
setlocal enabledelayedexpansion
set file=%userprofile%\Desktop\completelist.txt
set "dest=E:\destination" & set "i=" & pushd !dest!
for /f "usebackq tokens=*" %%G in ("%file%") do (
call :rename %%~nG %%~xG %%G
copy "%%G" "%dest%\!target!" >nul )
popd
exit /b
:rename
set "target=%1!i!%2"
:loop
set /a i+=1
if exist "!target!" set "target=%1!i!%2" & goto :loop
set "i=" & echo Copied %3 to !target!
exit /b
I have a folder with multiple photos in it with two different resolutions for each photo. I want to delete the photos with lower resolution. For example the folder contains the following files WP_20140917_19_15_04_Pro.jpg and WP_20140917_19_15_04_Pro__highres.jpg. I want to keep all the photos with the highres in the filename and delete the other one with use of CMD.
One important note is that it should only delete the photo if a highres version of the photo exists, because the folder does not always contains highres versions of the photos.
I know how to delete files when a certain word is repeatedly returned (such as highres), but I can't do the opposite, that is why I don't know how to start. All help is appreciated.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
set /a count=0
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*__highres.*" '
) DO (
SET "name=%%a"
SET "name=!name:__highres=!"
IF EXIST "%sourcedir%\!name!" (
ECHO(DEL "%sourcedir%\!name!"
set /a count+=1
)
)
echo %count% files deleted.
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
Uber-simple. Read the directory, targeting all files matching the pattern *__highres.*. Remove the __highres from the name and if a file exists with the modified name, delete it.
---edit : added-in count of deleted files.
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")