Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Batch file for %% variable/rename %%~nf.*

$
0
0

Hi

I have the batch script below which does the following:

1. recursively extracts .zip files (each zip contains only 1 file) using 7zip to a temp location

2. Each extracted file is renamed to the name of the .zip file preserving the extension - ren C:\DME\temp\*.* %%~nf.*

3. The file is then moved to new location which shares the same structure except the parent folder is slightly different. 

This all works fine except I have noticed one problem. 

ren C:\DME\temp\*.* %%~nf.*

This line renames the file to up to ".". If the file has a name with a number of "." I get a problem. Example = test.1.4.doc. My script renames this newfilename.1.4.doc instead of newfilename.doc

Because my %% variables apply to the original loop command (I think) I cant use %%~xI for example as this would just return .zip. I have tried a nested loop but enabledelayedexpansion does not seem to be working as %%f = the original file path.

Can anyone point me in the right direction?

Thanks

@echo off
setlocal enabledelayedexpansion
y:
cd "Y:\Scriptingv1\filepath\DME\data\00"

for /r %%f in (*.zip) Do ( 
	"C:\Program Files\7-Zip\7z.exe" e -aoa -oc:\DME\temp "%%f" 1>>C:\DME\logs\7ziplog.txt 2>&1
	Set currentpath=%%~pf
	Set newpath=!currentpath:Scriptingv1=Original!

	ren C:\DME\temp\*.* %%~nf.*
	move /Y C:\DME\temp\%%~nf.* "!newpath!"
	@echo on
	echo Moving file to !newpath!
)





Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>