Solved

Batch file command to find and rename folders

Posted on 2015-01-12
27
197 Views
Last Modified: 2015-01-14
See attached.  Need a couple of batch file commands:
1.  find and rename all folders found meeting the criteria
2.  a second batch command file to delete all occurences.
Better explained in attached.batch-help.doc
0
Comment
Question by:HFSCI
  • 14
  • 13
27 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 40545351
To rename CUR to HOLD:
Note: Remove echo prefix to run for real.
@echo off

pushd c:\users

for /d %%a in (*.*) do (
  echo if exist "%%a\profile_backup\%acur\nul" ren "%%a\profile_backup\%%acur" "%%a\profile_backup\%aold"
)
popd

Open in new window


To delete HOLD:
Note: Remove echo prefix to run for real.
@echo off

pushd c:\users

for /d %%a in (*.*) do (
  echo if exist "%%a\profile_backup\%%ahold\nul" rd /s /q "%%a\profile_backup\%%ahold"
)
popd

Open in new window

0
 

Author Comment

by:HFSCI
ID: 40546539
Tried the below for the first part and it did not work:
ren the below is the drive of the user list
pushd E:\WWT\Home\FortMyers

for /d %%a in (*.*) do (if exist "%%a\profile_backup\%acur\nul" ren "%%a\profile_backup\%%acur" "%%a\profile_backup\%aold")
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40546717
Can you post the ECHO method results similar to how I had it?
0
 

Author Comment

by:HFSCI
ID: 40546889
The results are attached.  I only have two setup with cur to test renaming them so as not to effect all of them.
batch-results.docx
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547118
My bad. For the first part, %a should be %%a:
echo if exist "%%a\profile_backup\%%acur\nul" ren "%%a\profile_backup\%%acur" "%%a\profile_backup\%%aold"

Open in new window

0
 

Author Comment

by:HFSCI
ID: 40547163
sorry but still didn't change the folder.  See attached for info
batch-result2.docx
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547208
First, please change \NULL to \NUL:

Other than that, it seems to be working. The ECHO is for debugging purposes and provides  visual confirmation only. To run it for real, remove the ECHO prefix as I mentioned originally.
0
 

Author Comment

by:HFSCI
ID: 40547275
sorry but still did not change maleyecur folder to maleyeold folder.  See attached
batch-result3.docx
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547288
Are you getting errors?
Please post the ECHO results
0
 

Author Comment

by:HFSCI
ID: 40547292
no errors shown the results are the same as shown in batchresult2 attachment
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547307
You need to remove the ECHO to run it for real, i.e. rename the folders:
echo if exist "%%a\profile_backup\%%acur\nul" ren "%%a\profile_backup\%%acur" "%%a\profile_backup\%%aold"

Open in new window

becomes...

if exist "%%a\profile_backup\%%acur\nul" ren "%%a\profile_backup\%%acur" "%%a\profile_backup\%%aold"

Open in new window

0
 

Author Comment

by:HFSCI
ID: 40547336
Below is my batch and it still did not rename the folder and there were no erros and attached the cmd window.  I manually went to the folder in the cmd window and renamed it from cur to old so know its not a permissions issue.


rem the below is the drive of the user list
pushd E:\WWT\Home\FortMyers

for /d %%a in (*.*) do (if exist "%%a\profile_backup\%%acur\nul" ren "%%a\profile_backup\%%acur" "%%a\profile_backup\%%aold")

pause
batch-result4.docx
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547394
My apologies. I was ahead of myself. Try this:
for /d %%A in (*.*) do (
      if exist "%%A\profile_backup\%%Acur" (
            pushd "%%A\profile_backup"
            ren %%Acur %%Aold
            popd
      )
)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:HFSCI
ID: 40547414
sorry but where do I put that in the existing batch file?
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547421
Here's the whole thing:
@echo off

pushd c:\users

for /d %%A in (*.*) do (
  if exist "%%A\profile_backup\%%Acur" (
    pushd "%%A\profile_backup"
    ren %%Acur %%Aold
    popd
  )
)
popd

Open in new window

0
 

Author Comment

by:HFSCI
ID: 40547477
see attached as it gives path error
batch-result5.docx
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547575
Are you running the commands inside the FOR loop on one line? Don't you want to use what I gave you?
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547592
Here's a simpler one:
@echo off
pushd c:\users
for /d %%A in (*.*) do (
  if exist "%%A\profile_backup\%%Acur" ren "%%A\profile_backup\%%Acur" %%Aold
)
popd

Open in new window

0
 

Author Comment

by:HFSCI
ID: 40547601
Sorry, I am confused on what you are asking.  The last attachment shows you the batch file I used which I thought was using the FOR loop.  The result picture was just showing the btch run and I only have 2 folders that actually meet the criteria usernamecur and that's only for testing purposes.  they all will eventually have a cur folder.  So the batch_result5 attachment above shows it going thru each folder but when it does find a usernamecur it errors about the path.
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547620
Please run the last post.
0
 

Author Comment

by:HFSCI
ID: 40547622
no errors but did not rename.  see attached
batch-result6.docx
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40547658
My last post doesn't have the NUL
0
 

Author Comment

by:HFSCI
ID: 40547665
Great News it worked.  You are my new Hero.  I will try the delete portion tomorrow and let you know
0
 
LVL 23

Accepted Solution

by:
NVIT earned 500 total points
ID: 40547670
For the delete portion, don't use the original post, which has the NUL. Instead, use this one:
@echo off
pushd c:\users
for /d %%a in (*.*) do (
  if exist "%%a\profile_backup\%%ahold" rd /s /q "%%a\profile_backup\%%ahold"
)
popd

Open in new window

0
 

Author Comment

by:HFSCI
ID: 40548816
Just wanted to say thanks for all your help and patience.  Will now accept your solution
0
 

Author Closing Comment

by:HFSCI
ID: 40548825
Exceptional help and patience.  Expert explained the process which also helped in understanding the solution
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40549265
Hi HFSCI...
Thanks for the quick update. I'm glad it eventually worked for you. Thanks for your patience, too!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Reading variable length EBCDIC in SAS 9 74
mapShare challenge 13 68
groovy example issue 10 65
tidtcpserver connection lost handle 2 41
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now