Re: Need Help with a DOS script (if possible)

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



The trick here is to approach the problem one step at a time. Run this batch
file this time:

@echo off
set Target=\\Server\Server F Drive\Need to Sort
set ext=.xls .doc
echo Checking the target folder xxx%Target%yyy
pause
dir /b /s /a-d "%Target%"
pause
for /F "delims=" %%a in ('dir /b /s /a-d "%Target%" ^| findstr /v /i
"%ext%"') do echo del "%%a" & pause

The program won't attempt to delete any files but it will echo the full
"delete" command to the screen, then pause. Now do this:
1. Press Ctrl+C to stop the batch file.
2. Examine the command carefully. Is it correct? If not, what's wrong?
3. Retype the command manually, letter by letter. Does it work? If not, why
not?


"Kardon Coupé" <prefer.to@xxxxxxxxxxxxxxxxx> wrote in message
news:uxgKaW7PJHA.1164@xxxxxxxxxxxxxxxxxxxxxxx
It correctly displayed the directory in the command window, but failed to
delete all 'other' files.

"Pegasus (MVP)" <I.can@xxxxxxxxxx> wrote in message
news:umh1ge5PJHA.3884@xxxxxxxxxxxxxxxxxxxxxxx
Fine. Now run your script like so:
@echo off
set Target=\\Server\Server F Drive\Need to Sort
set ext=.xls .doc
echo Checking the target folder xxx%Target%yyy
pause
dir /b /s /a-d "%Target%"
pause
for /F "delims=" %%a in ('dir /b /s /a-d "%Target%" ^| findstr /v /i
"%ext%"') do del "%%a"

The script will do a basic "dir" command on your target. Nothing flash,
nothing fancy. It will then pause. Does it generate the expected output?
If no then your folder name \\Server\Server F Drive\Need to Sort is
incorrect. Perhaps it has an invisible trailing space.



"Kardon Coupé" <prefer.to@xxxxxxxxxxxxxxxxx> wrote in message
news:ORVUm84PJHA.1168@xxxxxxxxxxxxxxxxxxxxxxx
@echo off
set Target=\\Server\Server F Drive\Need to Sort
set ext=.xls .doc
echo %Target%
echo %ext%
for /F "delims=" %%a in ('dir /b /s /a-d "%Target%" ^| findstr /v /i
"%ext%"') do del "%%a"

Like I said, I added in echo's for the command prompt.

"Pegasus (MVP)" <I.can@xxxxxxxxxx> wrote in message
news:eW3lKFzPJHA.3884@xxxxxxxxxxxxxxxxxxxxxxx
Let's have a look at your version of the script!

"Kardon Coupé" <prefer.to@xxxxxxxxxxxxxxxxx> wrote in message
news:e7y7ZBzPJHA.420@xxxxxxxxxxxxxxxxxxxxxxx
I think I might be doing something wrong, I see the CMD window pop up,
but it is too quick, and the files are still there....

So I opened up Command Prompt and ran it from there, and I got "File
Not Found", I'm thinking it is something to do with working on a
Network Drive.

I added in echo %Target% and echo %ext% just to see whether it was
only taking part of the folder because of the spaces, and it was
outputing the full foldername??

Will this work on either network drives, or drives that have been
mapped to a letter... also if the folder name has spaces in?

Sorry to be a pain...
Regards
Paul.

"Pegasus (MVP)" <I.can@xxxxxxxxxx> wrote in message
news:eKPjqjyPJHA.5076@xxxxxxxxxxxxxxxxxxxxxxx

"Kardon Coupé" <prefer.to@xxxxxxxxxxxxxxxxx> wrote in message
news:e6cANByPJHA.4504@xxxxxxxxxxxxxxxxxxxxxxx
Dear All,

I'm looking for a way using a DOS batch file to take a folder and
search everything within it, and delete every file except files with
a .xls or a .doc extension, also work recursively within the folder,
so if any subfolders are found it will deal with them too.

Is it possible, I'm usually the end person who receives files from
work and all I need is the .xls and .doc files, nothing else...

Is this possible... Ideally, I would like to right click on the
folder and use the 'SendTo' option and have the shortcut to the
batch file in there..

I've tried looking around Google (etc) for 3rd party software, but
nothing has jumped out at me (maybe I'm putting the wrong search
terms in)

Hope someone can help me with this matter as I'm no good using
DOS...

Thanks in advance.
Regards
Paul.

Unfortunately DOS cannot do this sort of thing. But then DOS is a
legacy operating system introduced some 30 years ago that is only
rarely used these days. However, you can easily perform the task with
a few Command Console commands under Windows XP. Here is a batch file
solution for you:
@echo off
set Target=D:\My Documents
set ext=.xls .doc
for /F "delims=" %%a in ('dir /b /s /a-d "%Target%" ^| findstr /v /i
"%ext%"') do echo del "%%a"

Do not retype the file - use copy & paste instead. Note that the
fourth line is a long line. It starts with [for] and ends with [del
"%%a"].

You must adjust lines 2 and 3 to suit your specific requirements. To
activate the batch file, remove the word "echo" in line 4.













.



Relevant Pages

  • Re: how to execute batch file automatically when file is added in folder
    ... Pegasus (MVP) wrote: ... Line2 if not exist c:\OldDir.txt echo. ... Line7 rem Your batch file lines go here ... I cannot tell you what "d:My Folder" should be. ...
    (microsoft.public.windowsxp.general)
  • Re: How to Re-name a Massive Number of Files
    ... You can do this with a humble batch file, ... set Source=d:\My Folder ... echo ren "%%a" ER!Number!.pdf ... Now did you say you were prepared to pay real money for ...
    (microsoft.public.windowsxp.general)
  • Re: Need Help with a DOS script (if possible)
    ... echo Checking the target folder xxx%Target%yyy ... The script will do a basic "dir" command on your target. ...
    (microsoft.public.windowsxp.general)
  • XP batch File, command line Zip, how?
    ... will, zip a folder using XP?s own ... Below is an example of my simple Batch file: ... @echo off ... copy TestSource\*.* TestDest ...
    (alt.os.windows-xp)
  • Re: Creating a .bat file?
    ... echo Merry Christmas ... If you want help with the batch file you need to reveal more of its contents or you should reveal the source of the file. ... 'Delay' is not a valid Windows command, this is probably just pinging the loopback adapter for 30 seconds. ... wouldn't know their user names or profile folder path so they would ...
    (microsoft.public.windowsxp.help_and_support)