Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

batch problem: delayed expansion not working?


  • Please log in to reply
3 replies to this topic

#1 karatechops4u

karatechops4u

  • Members
  • 5 posts
  • OFFLINE
  •  
  • Local time:01:40 AM

Posted 12 May 2010 - 10:21 AM

I'm trying to create a batch that finds files called XXXX-XXXX-Amd.txt and renames them to XXXX-XXXX.txt. I think the problem has something to do with the "setlocal ENABLEDELAYEDEXPANSION" command not working, but I only dabble in batch writing so I need some help.

here's the beginning of the code, just to get the correct filename (XXXX-XXXX instead of XXXX-XXXX-Amd) into the variable:

[codebox]
setlocal ENABLEDELAYEDEXPANSION
for /f %%i in ('dir /b *Amd.txt') DO (
set filename = %%~ni
echo !filename!
set filename = !filename:~0,-4!
echo !filename!
pause
)
endlocal
[/codebox]

The only output is "ECHO is on." and it doesnt appear to recognize the "!filename!" variable at all. The script runs exactly the same if I take out the setlocal and replace the ! with %.

Any advice/direction would be greatly appreciated.

BC AdBot (Login to Remove)

 


#2 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:40 AM

Posted 12 May 2010 - 05:43 PM

Hello.

There was another member with a similar problem here:
http://www.bleepingcomputer.com/forums/ind...t&p=1749751

Using another tool would be easiest, but if you want to batch it, it would be:
@ECHO OFF

FOR /F "tokens=*" %%F in ('DIR /B /A:-D *Amd.txt') DO (
	CALL :Rename "%%F"
)

:Rename
Set "File=%~1"
REN "%File%" "%File:~0,9%.txt"
The script will, for each file matching *Amd.txt' in the immediate directory, rename the file only to the first 9 characters + .txt.

Working with the namespaces in batch files is a pain, so it's best to create a "function".

With Regards,
The Panda

#3 karatechops4u

karatechops4u
  • Topic Starter

  • Members
  • 5 posts
  • OFFLINE
  •  
  • Local time:01:40 AM

Posted 20 May 2010 - 01:59 PM

Panda,

Thanks so much! Sorry to reply so late, got married in the meantime and lost track of this project, hah. Thanks again.

#4 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:40 AM

Posted 20 May 2010 - 03:41 PM

Congratulations!

Glad I could help.

The Panda




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users