Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
brink668
- Posts: 3
- Joined: 26 Feb 2013 20:41
#1
Post
by brink668 » 26 Feb 2013 20:45
Need some help coding this in BAT.
The script is written so that it does a recursive search with WMI to delete specific files on the the fixed drives.
I would like to know how to make it so that it has logic like the following, I just can't seem to figure this out how to add this in the script below
IF folder above "fileName0.url" == blue && AND && fileName == fileName0.url
Then Delete file fileName0.url
else if - ignore
Code: Select all
@echo off
Title Search and Delete Specific File
::Find out what disks are on the system.
for /f "usebackq skip=1 tokens=1" %%a in (`wmic logicaldisk get deviceid`) do (
::Make sure we only use the fixed disks
for /f "usebackq tokens=2 delims=:- " %%x in (`fsutil fsinfo drivetype %%a`) do (
::Is %%a, a fixed disk?
if "%%x" == "Fixed" (
echo.
echo - - - - - - - - - - - - - - - - - - - - - - - - -
echo Searching drive %%a for Startup Shortcuts
del /s/f/q "%%a\fileName0.url"
del /s/f/q "%%a\fileName1.url"
)
)
)
)
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 26 Feb 2013 22:15
This creates a file with all the desired filespecs which have 'blue' (case sensitive) in the path and then exits with the GOTO: EOF for you to examine the "%userprofile%\desktop\templist.tmp2" file and ensure it is created ok.
If it's what you want to delete then remove the GOTO :EOF and run it again.
The next code block will delete all the files - not exactly what you asked for but only you will know if it is sufficient for your needs.
Code: Select all
@echo off
Title Search and Delete Specific File
del "%userprofile%\desktop\templist.tmp" 2>nul
::Find out what disks are on the system.
for /f "usebackq skip=1 tokens=1" %%a in (`wmic logicaldisk get deviceid`) do (
rem Make sure we only use the fixed disks
for /f "usebackq tokens=2 delims=:- " %%x in (`fsutil fsinfo drivetype %%a`) do (
rem only used fixed disks
if "%%x" == "Fixed" (
echo.
echo - - - - - - - - - - - - - - - - - - - - - - - - -
echo Searching drive %%a for Startup Shortcuts
dir /s /b /a-d "%%a\fileName0.url" "%%a\fileName1.url" >> "%userprofile%\desktop\templist.tmp"
find "blue" ^<"%userprofile%\desktop\templist.tmp" >"%userprofile%\desktop\templist.tmp2"
del "%userprofile%\desktop\templist.tmp"
)
)
)
)
goto :EOF
for /f "delims=" %%a in ('type "%userprofile%\desktop\templist.tmp2"') do (
del "%%a"
)
del "%userprofile%\desktop\templist.tmp2"
-
brink668
- Posts: 3
- Joined: 26 Feb 2013 20:41
#3
Post
by brink668 » 01 Mar 2013 09:23
What happens if I wanted to search the drive for a specific file than replace that file in the same location with another file.
This is what I have so far, it deletes the correct file but I am having trouble copying a file from a network location to the same location of where the file was found originally and needs to be replaced.
Code: Select all
@echo off
:: Search C: drive for specific file. Delete file and Copy over new shortcut
SET PROCESSFILE=TheFileName
FOR %%A IN (C:) DO (
IF EXIST %%A\ (
FOR /F "delims=" %%B IN ('DIR /S /B "%%A\*%PROCESSFILE%*"') DO (
::ren "%%B" "%%~.BACKUP" >NUL
del "%%B" >NUL
XCOPY "\\UNCservername\TheFileName.lnk" "%CD\%%B" >NUL
::COPY "%CD%\%NEWFILE%" "%%B" >NUL
GOTO END
)
)
)
ECHO File not found
PAUSE
:END
-
brink668
- Posts: 3
- Joined: 26 Feb 2013 20:41
#5
Post
by brink668 » 01 Mar 2013 09:57
It was ( i guess all the information is there) just having a hard time getting it to work correctly.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#6
Post
by foxidrive » 01 Mar 2013 10:51
brink668 wrote:What happens if I wanted to search the drive for a specific file than replace that file in the same location with another file.
This should work - but as it stands with the GOTO :END it will only replace the first file found (if there are more than 1 of the files on the drive).
Code: Select all
@echo off
:: Search C: drive for specific file. Delete file and Copy over new shortcut
SET PROCESSFILE=TheFileName
FOR %%A IN (C:) DO (
IF EXIST "%%A\" (
FOR /F "delims=" %%B IN ('DIR /S /B /A:-D "%%A\%PROCESSFILE%"') DO (
ren "%%B" "%%~nB_BACKUP%%~xB"
:: del "%%B"
XCOPY "\\UNCservername\TheFileName.lnk" "%%~dpB\" >NUL
:: COPY /b "\\UNCservername\TheFileName.lnk" "%%~dpB\" >NUL
GOTO :END
)
)
)
ECHO File not found
PAUSE
:END