I have batch script that runs in WIn 7 command prompt.
it needs to delete all the files in folder except file with ".a" extension.
I tried following code
for /F %%I in ("*") DO if not (%%~xI=="a") del /q %%I
It deletes all files.
I tried following:
for /F %%I in ("*") DO if not (%%~xI==a) del /q %%I
no luck. Where am I wrong?
sedy
for /F loops consider sets in quotes to be strings, so you are saying to parse the string "*", which results in the delete command being interpreted as del /q *, which deletes everything.
Use for /F %%I in ('dir /b') instead, which will process based on the list of files in the current directory.
for /f "usebackq tokens=*" %%f in (`dir /b .\d\*`) do (
if not %%~xf equ .a (echo deleting %%f)
)
Related
I need to make batch script, which will work this way:
There is a folder with one hundred files. I need to make a script which is zipping every 4 files into single archive. Then all created archives must be zipped into one, big archive file. That must be all done by one script.
I tried to make it, but I haven't idea how to make it this way.
This is what I wrote first:
#ECHO ON
SET SourceDir=C:\Users\Ridaan\Documents\sobol
SET DestDir=C:\folder\Destination
CD /D "C:\Program Files\7-Zip"
FOR /F "TOKENS=*" %%F IN ('DIR /B /A-D "%SourceDir%"') DO (
7z.exe a "%DestDir%\%%~NF.zip" "%SourceDir%\%%~NXF"
)
EXIT
And second:
#echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
robocopy C:\folder\Destination /COPY:DAT /V /XO /NJH /NP /R:1000 /W:10
7z u -mx9 "C:\folder\End.7z" "C:\folder\Destination"
rmdir C:\folder\End\ /Q /S
:END
pause
Just to give you an idea of counting files and
calculating the int of count/4 for the zip number
the modulus%4 for the file number (0..3) to determine if it's a new zip to create or to append to the zip.
:: Q:\Test\2018\11\11\SO_53251220.cmd
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
set Cnt=0
Set "SrcDir=X:\Your\Path"
For /F "delims=" %%A in ('Dir /B/A-D "%SrcDir%\*"') do (
Set /A "File=Cnt%%4,Zip=Cnt/4,Cnt+=1"
Echo Cnt=!Cnt! File=!File! Zip=!Zip! File=%%A
)
I am not good in this. I really need help.
I have searched a lot but no luck.
I want to create empty files and replace existing in a folder and its subfolders.
From my search, I could find how to make a batch to delete.
set folder="test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
However, I want to replace the files with empty one, I tried this:
set folder="test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (echo. 2>"%%i")
But it is not working. I need this to replace all my existing files so after delete it should not be recovered.
Better quote the whole set command and use always quotes:
set "folder=x:\test"
cd /d "%folder%" ||(pause&Goto :Eof)
for /F "delims=" %%i in ('dir /b/A-D') do Type Nul >"%%i"
To not by mistake clear the current folder I added the ||(pause&Goto :Eof) in case the cd didn't work. This is a conditional execution on fail of the previous command
I have thousands of generated php files in a folder that are not necessary. They all having the same naming structure of 10 characters each example "rk9qiaLOaf.php".
But I don't want to delete all because the folder contains index.php,main.php such important files and I don't want them to be deleted.
Please, how can it be done to delete the files within a folder they all are having name count of 10 by a batch script. Thank you for reading my problem.
You could just use this from the Command prompt:
For /F "Delims=" %A In ('Where/F "C:\Users\Aung\Documents:??????????.php"') Do #Del %A
Just change the folder path as appropriate.
Edit
A recursive version, (could take a while).
For /F "Delims=" %A In ('Where/F /R "C:\Users\Aung" "??????????.php"') Do #Del %A
Once again just change the root folder, (use . for the current directory) as necessary.
You could use dir together with findstr to filter for the correct files:
dir /B /A:-D "*.php" | findstr /I "^..........\.php$"
Every dot . matches a single character. ^ and $ ensure to match the whole file name.
To delete the returned files, use this in command prompt cmd:
for /F "delims=" %F in ('dir /B /A:-D "*.php" ^| findstr /I "^..........\.php$"') do #del "%F"
Or this in a batch file:
for /F "delims=" %%F in ('dir /B /A:-D "*.php" ^| findstr /I "^..........\.php$"') do del "%%F"
And here is an alternative batch file approach relying on sub-string expansion:
#echo off
setlocal DisableDelayedExpansion
for /F "delims=" %%F in ('dir /B /A:-D "*.php"') do (
set "NAME=%%~nF" & set "EXT=%%~xF"
setlocal EnableDelayedExpansion
rem // Check whether file name is not longer than 10 characters:
if "!NAME!"=="!NAME:~,10!" (
rem // Check whether file name is longer than 9 characters:
if not "!NAME:~9!"=="" (
del "!NAME!!EXT!"
)
)
endlocal
)
endlocal
i have batch file which need to copy 3 last modified files from 3 different sources and need to rename it by removing last 33 characters.
i made it in 2 files but from som reason when i put both codes together its not working...
my code :
#echo off
set folderpath=C:\Users\tzahi.k\Desktop\testSource\des
for /F "delims=" %%a in ('dir /b /od "C:\Users\tzahi.k\Desktop\testSource\source\*.txt"') do set Youngest=%%a
xcopy /y "C:\Users\tzahi.k\Desktop\testSource\source\%Youngest%" %folderpath%
for /F "delims=" %%a in ('dir /b /od "C:\Users\tzahi.k\Desktop\testSource\source2\*.txt"') do set Youngest=%%a
xcopy /y "C:\Users\tzahi.k\Desktop\testSource\source2\%Youngest%" %folderpath%
cd %folderpath%
for /f %%a in ('dir /b "%folderpath%\*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
pause
when i split the code in 2 files it works but i want it in one...
any suggestion?
i use
setlocal EnableDelayedExpansion
and it works
Need to read files in directory, rewrite them after skipping the first 77 lines, save to new txt file with the old name.
Diectory (there are many more files than this):
{1061A083-F913-4F7A-AEC4-E89BD7FEAC47}.html
{275A93DF-997B-4B2B-B5D6-C66302A03508}.html
{41579A2D-C022-44BE-9752-5407D241BBE2}.html
{47339F9D-AC59-433F-9FEB-1E818C7C1904}.html
{513E7E93-F6D5-4F1F-A905-28FE4D3DB30C}.html
Code I have so far:
for /f "skip=7 delims=" %%a in ('dir /b *.html""') do (echo %%a>>newfile.txt)
xcopy newfile.txt C:\MBCNew\htmlFiles\Done\%%a.txt /y
del C:\MBCNew\htmlFiles\newfile.txt /f /q
The following simple batch script is very efficient (fast). It translates tab characters into spaces, but that should not be a problem for HTML.
#echo off
for /f "eol=: delims=" %%F in ('dir /b *.html') do (
more +77 "%%F" >"%%F.new"
move /y "%%F.new" "%%F"
)