win cmd: Deleting files that have no sidecars - batch-file

Having troubles with cmd syntax, trying to delete files with specific extension in a specific folder that don't have sidecars. eg.: if folder contains:
1.A, 1.B, 2.A, 3.A, 4.A, 4.B
bat should only delete
2.A, 3.A
..hope that makes sense.
The code I've got so far must be real close, unfortunatelly not working
#echo off
FOR %%x IN (%1\*.A) DO
(
IF not exist "%1\%x.B" del "%1\%x.A"
)
Any help mostly appreciated.

Comments to question are correct
FOR variable percents must be consistently doubled when used in batch script
Open paren must be on same line as DO. But there isn't any need for parens with such a simple script.
Also, you want only the base name of the FOR variable, so you need the ~n modifier.
I made the code a bit more robust by using PUSHD at beginning.
#echo off
pushd %1
for %%F in (*.A) do if not exist "%%~nF.B" del "%%F"
popd

Related

Recursive rename isn't working at all in batch

So I have been searching in other topics how to rename files and folders in a .bat in a recursive way but it isn't working at all.
My code is:
# echo off
setlocal enabledelayedexpansion
set /p rut="Introduce folder: "
FOR /D /R %%x in ("%rut%"\*) DO (
cd %rut%
echo %cd%
pause
ren .\* "a"
)
exit
But this only renames the files that are on the first folder and not in the rest. Forfile won't work at all with the variables.
What I want to get is how I could rename everything inside the main folder (including subfolders) whatever it is, be it files or folders as "a" to solve the Windows problem of having routes way too long when trying to delete a full structure, that is why I can't use the ".txt" ".whatever" solution.
A powershell solution would be valid too!
Thank you very much
Edit I added a random to see if it was the name that was conflicting but no, it is still not working:
ren .\* "a%RANDOM%"
And renaming them from cmd works the same way, I mean, if I write ren "folder" "whatever" it will change but in the script doesn't work with "*"

Batch script rename file

I have a file called xxxxxxx_12345.pdf and I try to delete the suffix and the result would be xxxxxxx.pdf.
I tried following:
forfiles /S /M *_12545.pdf /C "cmd /c rename #file #fname*.pdf"
but it could not change the file name.
Can somone help to solve this issue?
thanks
I'm sure there is a way to do the job with forfiles command, however, it would be much easier to use regular for loop for this.
#echo off
pushd %~dp0
setLocal EnableDelayedExpansion
for %%f in (*_12545.pdf) do (
set "CurrentFileName=%%~nf"
set "RenameTo=!CurrentFileName:~0,-6!"
echo.ren !CurrentFileName!%%~xf !RenameTo!%%~xf
)
pause>nul
By setLocal EnableDelayedExpansion, user can use delayed environment variables, which will enable user to use something like !variable! instead of %variable%.
set "CurrentFileName=%%~nf"
%%~nf will give you the name of the file without extension, so in this case, CurrentFileName will be FileName_12545. Type for /? to see the full explanation of the syntax.
set "RenameTo=!CurrentFileName:~0,-6!"
Notice that I used !variable:~0,-6! to remove suffix. This is String Manipulation. The user is storing a string without suffix that has a length of 6 (which is _12545 in this case).
echo.ren !CurrentFileName!%%~xf !RenameTo!%%~xf
I placed echo in front of ren so you can check before you actually rename it. %%~xf will return the file extension including .(dot) in front of it. In this case, the file extension is .pdf.
I hope this help a bit.

batch file : overwrite one file to several existing files

I am trying to make simple security program for my company.
We usually make a lot of doc or ppt(x) files and
for some reason, we need to make them disable as soon as possible.
So we usually deleted the all of files but It took so long.
So I thought I can do that by overwritting the files.
If I have a empty doc or ppt files then overwite all of doc, ppt files in working drive each, then it will be faster and much safer than just deleting.
So I tried to use xcopy
Assuming empty.doc is just empty doc file and
xcopy /s /y c:\users\mycom\empty.doc c:\*.doc
But it said cannot perform cyclic copy
I need you guys help
and I am glad to hear suggestion.
Thanks.
This is an old, old batch file that I employed for such an endeavour:
:: dispose.bat
#echo off
:: Check parameter(s), stripping quotes
set yp1=%1
set ydf=
:insistp1
if defined yp1 set yp1=%yp1:"=%
if not defined yp1 for %%i in (echo goto) do %%i error - filename required
if not exist "%yp1%" for %%i in (echo goto) do %%i error - file not found
for %%i in ("%yp1%") do (set yfr=%%i&call :zapit %%~di) 2>nul
:error
:: Clean up variables used
if defined ydf echo Warning! dispose failed!!
for %%i in (yp1 yfr yrn ydf) do set %%i=
goto :eof
:zapit
set yfr=%yfr:"=%
IF /i %1 == u: DEL "%yfr%" &goto :eof
if not exist %1\delete\. md %1\delete
(set yrn=)
:rndloop
set yrn=%yrn%%random%
if exist %1\delete\%yrn% goto rndloop
if not exist "%yfr%" set ydf=Y&goto :eof
move "%yfr%" %1\delete\%yrn%>nul
goto :eof
:: dispose.bat ends
Noting that u: is a RAMDRIVE on my system, hence mere deletion is all that is required.
The purpose is not to actually delete the files, but to move them to a directory named ?:\delete and provide them with a random name in that directory.
Since the file is simply MOVEd it is quite fast, which addresses your time consideration.
An issue for me is the idea of copying a file over all of the files you target. If the file that you copy is shorter than the other files, some data wilstill be available to be recovered. Regardless, it will alwats be slower than simply deleting the files (which you say you are currently doing.)
This scheme simply accumulates the files-to-be-deleted in a known directory on the same drive (so they will simply be moved, not copied.)
Once they are in your \delete directory, you can let a utility like ccleaner or recuva loose on that single directory in the background and it will overwrite the files a specified number of times.
Here's a simpler method. Be careful.
At the command line:
for /r c:\ %A in (*.doc? *.ppt?) do echo. > %A
In a batch file:
for /r c:\ %%A in (*.doc? *.ppt?) do echo. > %%A
EDIT:
To replace with a file, see the example below. Replace the example's d:\path\file.ext with your intended file. Note that the previous option will work much faster with a similar result.
At the command line:
for /r c:\ %A in (*.doc? *.ppt?) do copy d:\path\file.ext > %A
In a batch file:
for /r c:\ %%A in (*.doc? *.ppt?) do copy d:\path\file.ext > %%A
Either way, as noted in Magoo's answer, larger files will still have recoverable data on the drive. You stated in a comment:
But if I overwrite the original files, then they cannot guess what it
was unless they got bak files
This isn't accurate. Forensic tools can retrieve the partial data that wasn't overwritten with new content.

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

Batch file to recursively move a single subdirectory from multiple parent directories TO another single, defined directory

If that title wasn't confusing enough.. hopefully what I am trying to do is a lot simpler to understand.
Windows 7 just in case it needs to be said.
I have multiple directories within the folder I am working in;
C:\WorkingDir\1
C:\WorkingDir\2
C:\WorkingDir\3
and so on
Within each of these folders (1,2,3,etc) is a single sub-directory and no other files or folders;
C:\WorkingDir\1\5E04AB
C:\WorkingDir\2\4F07FC
C:\WorkingDir\3\9DA04F
I need to move each of those single subdirectories from within the parent folder to a new folder;
C:\NewFolder\5E04AB
C:\NewFolder\4F07FC
C:\NewFolder\9DA04F
That's it! I thought it might be simple, but I can't wrap my head around the variables or a better resource explaining how to use them. I don't use batch files much if at all, so I am very sorry for this cry for help. Hopefully someone more knowledgeable has a simple explanation that can help me out :-)
I found this: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
But can someone link me to a resource where I can learn more about batch variables and parameters for future reference?
Thank you thank you thank you
Update:
#endoro
Thanks for your response. There must have been a user error the first time I tried to run your code. It is working properly and all is well! Thank you so much!
Update 2
After running the code User1 contributed, It will create my NewFolder directory, but will not copy anything to it. It remains empty. Here is some of the repetitive output in DOS when I run it:
C:\WorkingDir>(
set fldr2=C:\WorkingDir\1\5E04AB
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\2\4F07FC
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\3\9DA04F
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.`
Please look at the output and remove the echo, if it is OK:
#echo off &setlocal
set "workingdir=WorkingDir"
md "C:\NewFolder" 2>nul
pushd "%workingdir%"
cd
for /d %%i in (*) do for /d %%j in ("%%~i\*") do echo move "%%~fj" "C:\NewFolder\%%~nxj"
popd
I haven't been able to test this but:
#echo off
setlocal EnableDelayedExpansion
md "c:\NewFolder\"
cd "C:\WorkingDir\"
for /D /r %%a in ("*") do (
set fld1=%%a
cd "%fld1%"
for /D /r %%b in ("*") do (
set fld2=%%b
move "c:\WorkingDir\%fld1%\%fld2%" "C:\NewFolder\%fld2%"
)
)
And a good resource for batch:
http://ss64.com/nt/

Resources