• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1161
  • Last Modified:

Batch file for wild cards in RD command?


I inherited a file syncronisation system which adds a tag of ".pstmp" to certain directories every day. Within a short period of time you end up with directory names which are 28973287328973 characters long and unusable by most software eg
day1 hello.pstmp
day2 hello.pstmp.pstmp
day17 hello.pstmp.pstmp.pstmp.pstmp.pstmp.pstmp.pstmp.pstmp.pstmp.pstmp.pstmp.pstmp etc
Note it just does this with folders and not files (thank god!)

I can remove the bad folders manually via something like
dir /x (to get the 8.3 name)
rd adobe~~1.~ps /s (nb will only work with the actual 8.3 name, not wild cards it seems)

Sadly, wild cards dont appear to work with rd though, and there are several thousand of these folders, so doing them manually one at a time isn't appealing. Any batch file gurus know a way to automate this?
0
dxve
Asked:
dxve
  • 10
  • 8
  • 5
  • +3
2 Solutions
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
I THINK this will work (no need for a batch file)

for /f "tokens=5" %a in ('dir /ad /x ^| find "~"') do @rd %a /s

If you wanted to enclose that in a batch file, then change all %a references to %%a
0
 
dxveAuthor Commented:

Dang. That's an impressive first shot. It has 2 small problems though:

1) It's returning every directory, I think because the "~" we are searching for is used in all 8.3 abbreviations. I tried to change this to "~ps" but then it didnt work at all, so must be missing something there.

2) It comes back with "the system cannot find the file specified" for each attempted deletion. A confirmation y/n box shows that the rd is only being passed the file name, and not the extension.

I suspect this is close enough to troubleshoot, but incase you check this and they are easy tweaks..

Much appreciated, either way! :)
0
 
DrWarezzCommented:
hmm.. I'm not 100% certain on what exactly you're trying to achieve, but perhaps something like this would work:

:----8<-------------------- FileName.bat ---------------------------------:
@ECHO OFF
set directory=c:\directoryWithStupidSubs

CD %directory%

FOR /D %%D in (*) do @RD %%D /S /Q >>NUL

echo.
echo Complete.
ping -n 2 localhost >>NUL
exit /b
:----8<-------------------- FileName.bat ---------------------------------:

gL,
[r.D]
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
dxveAuthor Commented:

Almost identical result on that one:
1) it's hitting every directory
2) it can't delete any of them
so basically returns a "the system cannot find the file specified" for every directory

I can see this one is going to be a pain, so I'll post an additional 500 points if anyone can get it, for a total of 1000.
0
 
DrWarezzCommented:
Okay, let me review it again, and see if I can come up with another solution..

>"I can see this one is going to be a pain, so I'll post an additional 500 points if anyone can get it, for a total of 1000"
btw; you're not allowed to award more than 500 points for the one Q.. this is actually a member agreement violation (call "Point Passing"). And your account will probably be suspended if you commit it! :o\  (I found this out the hard way).

So, 500 points will do (not that we have much choice anyway) ;-)

gL,
[r.D]
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
I believe the problem was just some annoying typos - an extra " mark and an extra "i" in "directories" - try this

------------------------------------------
@echo off

Rem To set the file name without being prompted,
REM remove the "REM" from the beginning of the next line and change the abc.xyz to the file name you want to search for
REM set targetfile=abc.xyz
REM Then add a "REM" in front of the next occurance of the word SET

REM to be prompted for the file, leave the line below as is, otherwise, add a "REM" in front of it.
SET /p targetfile=Search for this file:

REM Collect the file locations
for /f "tokens=*" %%a in ('dir /s c:\%targetfile%') do @echo %%a >> filelist.txt
for /f "tokens=*" %%a in ('dir /s d:\%targetfile%') do @echo %%a >> filelist.txt

REM filter for Directory Names
For /f "tokens=3" %%a in ('find /i "Directory of" filelist.txt') Do @echo %%a >> directories.txt

REM Rename the existing files to a backup file name based on the current date and time
for /f "tokens=*" %%a in (directories.txt) do ren "%%a\%targetfile%" "%%a\%targetfile%.%date:~-4%%date:~4,2%%date:~7,2%.%time:~0,2%%time:~3,2%%time:~6,2%"

REM Copy the new file from the server
for /f "tokens=3" %%a in (directories.txt) do copy "\\server\share\path\to\file\%targetfile%" "%%a"

del filelist.txt directories.txt
------------------------------------------
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
I've been doing way too much of this today - that last post was for the wrong question - I'll amend this one soon though
0
 
SteveGTRCommented:
How about this:

@echo off

for /f "delims=" %%a in ('dir /s /b /ad 2^>NUL ^| findstr .pstmp') do echo rd /s /q "%%a"

Remove the echo statement to enable the rd.

Good Luck,
Steve
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Ok - Try this:

for /f "tokens=5" %%a in ('dir /ad /x ^| find "~"') do (
echo %%a >> dirs.txt
for /f "tokens=1" %%m in ('find /i "ps" dirs.txt') do rd %m /s
)
del dirs.txt
0
 
dxveAuthor Commented:

SteveGTR:
Your code looked promising, but does nothing that I can tell (with or without the echo). About the only problem I could see was using .pstmp instead of ~ps which is the 8.3 abbreviation. I tried changing this, but it still didnt do anything noticeable, so not sure if thats a problem or not.

leew:
Your code produces the txt file, fills it with (all) the directory names (good and bad ones) using the first word of the long name format name, then doesnt delete any of them (no doubt because the names are then all unrecognised). This gave a clue in that "dir /x /ad /b" produces the desired 8.3 results from a command prompt, whereas "dir /x /ad /b >> dirs.txt" lists the long name formats to the file. Don't you just love Windows.. maybe there's a need to activate 8.3 support within Windows? Testing this on a 2k Server box currently if relevant, but assume all flavors would act the same.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Here's what it SHOULD be doing:

(WOW - look what commenting does -it exposes the bugs in your code!  My last try from last night was flawed.  Try this.

REM First pull up a directory of all directories, listing their short file names and store them in a text file.
for /f "tokens=5" %%a in ('dir /ad /x ^| find "~"') do echo %%a >> dirs.txt
REM Find all directories from that text file that also contain "ps" in them and then remove them.
for /f "tokens=1" %%m in ('find /i "ps" dirs.txt') do rd %m /s
REM Delete the temp file with all the directory names.
del dirs.txt

If the above doesn't work, can you do a dir /x and post the first 10 lines of the directories listed - might help us figure out exactly what we're doing (I think I understand, but I get the feeling I'm missing something.
0
 
dxveAuthor Commented:

Just to clarify the problem, incase I missed something crucial: say there's a directory looking like this

folder1
folder2
folder2.pstmp.pstmp.pstmp.pstmp.. (over 255 characters worth, so inaccessable to most Windows software)
folder3 etc

For whatever reason the only command line tool which can remove these long name directories is rd, and I can sucessfully remove them manually with this by using the 8.3 name instead of the windows loooong name. However I have several thousand of them spread accross numerous machines and drive mappings, and wouldnt you know it, rd doesnt work with wildcards, so looking for a way to kill them efficiently.


0
 
SteveGTRCommented:
I see now that DOS rd can't handle the large directory. Does the default dir command produce the full directory name?

For example does:

dir

Produce:

...
99/99/9999  99:99a     <DIR>    folder2.pstmp.pstmp.pstmp...
...
0
 
slycoderCommented:
I would suggest to have these files written to a temporary path - then just purge the parent directory.

for instance
c:\dailytempfiles\folder1.pstmp.pstmp
c:\dailytempfiles\folder2.pstmp.pstmp
c:\dailytempfiles\folder3.pstmp.pstmp
c:\dailytempfiles\folder4.pstmp.pstmp


then just perform a "deltree" on c:\dailytempfiles

use deltree from a windows 98se installation disk in
the \TOOLS\oldmsdos directory

you also may have problems with your file allocation table, free sectors available and lost clusters.  You may want to have a third party software package to clean up what gets left behind.
0
 
dxveAuthor Commented:

SteveGTR:
Yes, the default dir appears to handle them (though I havent physically counted and compared to the windows equivalent) One issues is that within windows I cant rename them as the names are too long and give an error if right clicked, or accessed in any way. The main reason for wanting to use dos/batch files is because the only way I have found of being able to touch them is via their 8.3 names.

slycoder:
One problem I didnt initially highlight is that its very hard to manipulate these things, period. As part of moving them to the temp directory I would already be deleting from the places they currently live. These things arent user created, there was a syncronisation tool (PeerSync) which appears to have gone screwy at some point and spammed them until caught. There is a legitimate scenario where it should append a .pstmp to a folder name, just not hundreds of times for the same folder. Either way, I cant get them into a temp folder.. though if I could a deltree would be a fast solution.
0
 
SteveGTRCommented:
The reason I asked is that you said that my first coding attempt didn't print out anything. I see now that if it were to try and remove the directory it would fail because of the length of the directory name.

Just to clarify, running this:

@echo off

for /f "delims=" %%a in ('dir /s /b /ad 2^>NUL ^| findstr .pstmp') do echo rd /s /q "%%a"

Produces no output?

I'd like to first identify the files using the standard long file name and the string ".pstmp". Then I'd delete the file using the 8.3 name.
0
 
dxveAuthor Commented:

Yup, no output whatsoever.
0
 
SteveGTRCommented:
What does "dir /s /b /ad" display for one of these long directory names? Can you capture one and post it back here?
0
 
dxveAuthor Commented:
Sure thing.

If the number of pstmp stamps isnt too long for windows then it will return something like:

E:\server1\g\COMPAN~1\FILES\ch.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.
~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.
~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.
~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP.~PSTMP\Torres,G

If PeerSync has added too many however it will return an error saying "filename (ie as above) is too long" and just skip to  the next enty.
0
 
SteveGTRCommented:
Okay that makes sense. How about this:

@echo off

for /f "delims=" %%a in ('dir *.~ps /x /s /b /ad 2^>NUL') do echo rd /s /q "%%a"

Again, the rd has echo in front for testing.
0
 
MaartenGCommented:
Hello dxve,

you need to shorten the path (as if you didn't know :-)

Try this:

_____| SHORT.CMD |______________________

@echo off
echo on
for /D %%x in (*.~PSTMP) DO cALL :SHORT "%%x"
goto END

:SHORT
ren %1 SHORT.DIR
rd /s /q SHORT.DIR
GOTO :EOF

:END

_____| SHORT.CMD |______________________

It renames every .~pstmp folder in some short folder name and deletes that one.




If this fails too: I had a similar problem on on of our servers. My solution was (translated to your situation):

SUBST W: E:\server1\g\COMPAN~1\FILES (replace W: with a free driveletter)
It makes the path JUST a little shorter, which should do most of the times .....
Try any of the suggestions above. Most of them will work now ....

Afterwards, don't forget to remove the substittuted drive (SUBST w: /d)
0
 
DrWarezzCommented:
Hi Dxve,

Does any of this help at all? If so, then please accept the appropriate answer (or split points if necessary). If you continue to have problems however, please let us know what's wrong exactly, and we'll try and help you out further.

Thanks,
[r.D]
0
 
dxveAuthor Commented:

Warning: Accepted answers do not work!

This question was abandoned due to the repeated wrong guesses and no progress towards an answer.

Kind of retarded picking random answers and forcing an accept imo
0
 
SteveGTRCommented:
I suggest you click Support and post a question to have your points refunded.

Also, you might want to review the EE guildelines available by clicking Help. Perhaps that will help you to understand how EE works.

Good Luck :)
Steve
0
 
SteveGTRCommented:
Oh, I see that the administrator has forced an accepts (and one of the answers was mine --- yippee).

I suppose since you've failed to respond to the messages posted to this question the decision was made without your consent. If you had responded to the emails perhaps you wouldn't be in this situation.

Maybe you should chalk this up to a lesson learned and read the EE guidelines.


0
 
dxveAuthor Commented:

I have no problem with the points being awarded, certainly the experts worked hard to solve the issue and deserve something for that effort. However, as a high % of questions on this site have forced accept answers if these are picked at random from available guesses then the value of the site is much lower than it first appears. Ditto, the credibility of experts is lowered. EE guidlines or not, it's somewhat disturbing to discover.

I got pretty excited that there was a solution to this problem finally, then realized it was a forced accept of an ancient question of mine. Doh! lol
0
 
SteveGTRCommented:
You might want to review the timing in between when you posted and when an expert (or experts) responded. Solving these questions is a two way street. I believe you got exactly the correct results given your attention to this question.

Now that's just my opinion...
0
 
dxveAuthor Commented:

My response times ranged between 5 minutes and 24 hours until the point there was only one respondant, who couldnt get his code to work or predict the outcome of a generic dos command. It seemed more dilplomatic to abandon the question than pursue it at that point.

Posting a warning to future readers to save them wasting their time trying non-working solutions would appear the helpful thing to do in this scenario

Now that's just my opinion...
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 10
  • 8
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now