Delete Specific Extension files from the C: drive excepts from two folders - windows

I'm looking for a batch script commands that deletes all the files of specific extension (example .exe, .log) from my C: drive except from the two folders (folder_A and folder_B). Any idea how can I achieve this.
I'm using the below code but it's deleting the files from the manual folder also
If exist "D:\" (
for /D %%D in (*.*) do (
if /I not "%%~nxD"=="manual" del *.txt /s "%%~D"
)
pause
Thank you.

This will work for you:
If exist "D:\" (
for /D %%D in (*.*) do (
if /I not "%%~nD"=="manual" del /q /s "%%~D\*.txt"
)
)
pause
Note that you didn't also close the if exist code block.

Related

Copy and rename multiple files in multiple directories with windows CMD line

I want to copy and rename files that are stored in a stack of directories (year, month, day). The files are .ers files and I need them to maintain their extension but to go from file.ers to file_cloud.ers.
Then I would like the files for a given month to be saved in the cloud folder within each month.
Ideally I would do this in the Windows Command Line.
I understand a batch file is a good place to start, and so I have the following - this doesn't include saving in a different location - but I am struggling with just the copy and rename.
for /D %%y in ("C:\Data\20*") do (
pushd "%%~y"
for /D %%m in ("*") do (
pushd "%%~m"
for /D %%d in ("*") do (
pushd "%%~d"
for %%f in (soig_*.ers) do copy "%%~f.ers" "%%~f_cloud.ers"
popd
)
popd
)
popd
)
This might be easier than you are making it. If this echos the correct copy command, remove the echo. You get this by sticking your head way down into the output of FOR /?.
FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B /A:-D "*.ers"`) DO (
echo COPY "%%~f" "%%~dpnf_cloud.%%~xf"
)
The following should work okay for you, (it is supposed to copy all matching .ers files with updated names to the existing cloud directory at the previous level of their tree).
#FOR /D %%Y IN ("C:\Data\20*") DO #FOR /D %%M IN ("%%Y\*"
) DO #FOR /D %%D IN ("%%M\*") DO #IF /I NOT "%%~nxD"=="cloud" FOR %%F IN (
"%%~D\soig_*.ers") DO #COPY "%%F" "%%~M\cloud\%%~nFcloud%%~xF">NUL
I have not included anything to prevent overwrites of files whose names already exist within that cloud directory

Batch to move files outside of randomly-named subfolders

I have a data set consisting of files organised according to the following hierarchical folder/subfolder structure:
I would like to remove all nuisance subfolders (move its contents outside of it at the same hierarchical level + delete the nuisance folder), thus ending up with the files organised like this:
How can I achieve this, using a batch file, run from a command prompt inside Windows 7? I've tried a number of for statements with %F and %%F, but none worked. Grateful for any tips.
I believe the following will accomplish your goal if and only if all child folder and file names are unique. If there are duplicates, then all hell will break loose.
I have not tested, so backup and/or try the code on disposable data first.
You will have to modify the first PUSHD command to point to the root where all your "person n" folders reside.
#echo off
pushd "yourRootWherePersonFoldersReside"
for /d %%U in (*) do (
pushd "%%U"
for /f "eol=: delims=" %%A in ('dir /b /ad 2^>nul') do (
for /d %%B in ("%%A\*") do (
for /d %%C in ("%%B\*") do (
md "%%~nxC"
for /d %%D in ("%%C\*") do move "%%D\*" "%%~nxC" >nul 2>nul
)
)
rd /s /q "%%A"
)
popd
)
popd
The second FOR loop must be FOR /F instead of FOR /D because FOR /D has the potential to iterate folders that have been added after the loop has begun. FOR /F will cache the entire result of the DIR command before iteration begins, so the newly created folders are guaranteed not to interfere.

Batch scripting-cleanup script

i've engaged with work in batch script.these things i need to do
1.i want to find the folder say like "name" in particular directory
ex:
c:\test\name
c:\test\b\name
c:\test\n\c\name
2.in the name folder, need to delete all sub folders and files and all which is more than 90 days.
i have changed my question now please give me an idea...
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
ECHO(DEL "%targetdir%\*?*"
FOR /f "delims=" %%a IN (
'dir /b /ad "%targetdir%\*" '
) DO (
ECHO(RD /S /Q "%%~a"
)
GOTO :EOF
You would need to change the setting of targetdir to suit your circumstances. It could of course be replaced by a literal if you wish.
The required RD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(RD to RD to actually delete the directories.
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.
the del and for commands could be cascaded with a & if required. The for is spread across a nuber of lines for clarity.
You're missing an asterisk * so that the folder set expands to all subdirectories of the _delete folder. Also, the /D /R construct seems unnecessary on second thought, because RD /S does already take care of deleting directories recursively.
FOR /D %%A IN ( folder_you_want_to_clean\* ) DO IF EXIST "%%A" RD /S /Q "%%A"
But it won't delete the files in the folder_you_want_to_clean folder itself, though. Do that with the DEL command - the /Q option suppresses the confirmation prompt, just like with the RD command:
DEL /Q folder_you_want_to_clean\*
Concrete example: Suppose the folder you want to clean is C:\data\oldstuff. Then just do:
FOR /D %%A IN ( C:\data\oldstuff\* ) DO IF EXIST "%%A" RD /S /Q "%%A"
All directories contained in C:\data\oldstuff will be gone (and of course, any files in those directories). But any files in C:\data\oldstuff itself will still be there! So to delete those as well, do:
DEL /Q C:\data\oldstuff\*

xcopy batch folder tree

S:
cd \newclients
xcopy "s:\clients\*\MER" . /s /d
pause
This is my batch file. In my folder tree we have clients then names folders then MER folder. I want to be able to search the directory for the MER folder and copy that folder along with the client name before it. Is there a way to do this with batch files?
Using if exist should do the thing.
pushd "s:\newclients"
for /f "delims=" %%f in ('dir /b /ad "s:\clients\*"') do (
if exist "s:\clients\%%f\MER\nul" ( xcopy "s:\clients\%%f\MER" "s:\newclients" /s /d )
)
popd
pause
Note: Add a slash if it's to check folder ie. if exist "s:\clients\%%f\MER\" ... or if exist "s:\clients\%%f\MER\nul" ...
To suppress prompt about destination file/folder add /I in xcopy :
/I If in doubt always assume the destination is a folder
e.g. when the destination does not exist.
https://stackoverflow.com/a/33445866/

how to traverse specified subfolders in a windows batch file?

Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.
Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.

Resources