Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: File Prefix Help

03 Jun 2015   #1
born2achieve

windows7 64 bit
 
 
File Prefix Help

Hi,

I need to add the prefix to the files in specific directory. For this i need to input the directory name.

I need to add the prefix "Sample_" to all the txt files in the specified directory

Sample code i tried:
Code:
FOR /f "delims=" %%F IN ("D:\Sample\*.txt")  DO (RENAME "%%F" "Sample_%%F")
but it's not working. any suggestion please


My System SpecsSystem Spec
.
04 Jun 2015   #2
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Hi Born2achieve,

Here's a sample batch file that will help you achieve what you need.
Code:
@echo off

set prefix="Sample_"
set suffix=""

set location="D:\Sample"
set filemask="*.txt"

pushd "%LOCATION:"=%"
for /f "delims=" %%I in (' dir /a:-d /b "%LOCATION:"=%"\"%FILEMASK:"=%" ') do (
	if not "%~0"=="%%~fI" ren "%%~fI" "%PREFIX:"=%%%~nI%SUFFIX:"=%%%~xI"
)
popd

And to help you identify where you went wrong, here are the two corrected Command Prompt one liners you can compare your line with.
Code:
rem Using a For /F loop
for /f "delims=" %I in (' dir /a:-d /b "D:\Sample\*.txt" ') do ren "%~fI" "Sample_%~nxI"
rem Using an ordinary For loop
for %I in ( "D:\Sample\*.txt" ) do ren "%~fI" "Sample_%~nxI"
My System SpecsSystem Spec
04 Jun 2015   #3
born2achieve

windows7 64 bit
 
 

thank you gentle man. your batch code worked

is there any way to check if the already starts with "Sample_" and skip in that case adding the prefix.

thanks
My System SpecsSystem Spec
.

05 Jun 2015   #4
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Absolutely.

Code:
@echo off

set prefix="Sample_"
set suffix=""

set location="D:\Sample"
set filemask="*.txt"

pushd "%LOCATION:"=%"
for /f "delims=" %%I in (' dir /a:-d /b "%LOCATION:"=%"\"%FILEMASK:"=%" ') do (
	if not "%~0"=="%%~fI" (
		echo '%%~nI'| findstr /i "^'%PREFIX:"=%.*%SUFFIX:"=%'$" >NUL || ren "%%~fI" "%PREFIX:"=%%%~nI%SUFFIX:"=%%%~xI"
	)
)
popd
My System SpecsSystem Spec
07 Jun 2015   #5
born2achieve

windows7 64 bit
 
 

Hi Genleman,

your solution works perfectly. Much appreciated. Thanks a lot.
My System SpecsSystem Spec
Reply

 File Prefix Help




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
VB script: Rename all files in a folder to a specified prefix
Hi, I'm trying to find help in entering paths and prefix's in my vb script. I am of course a newbie to visual basic and don't know where to enter the path and wording for prefix. Any help would be most appreciated. Rich
General Discussion
Calculating the range of an ip address from a subnet mask/ prefix.
Hi there, I am doing the Microsoft 70/ 680 exam for my college course and i have an exam next week for networking stuff and calculating the range of an ip address from the subnet mask. I was wondering if any of you guys can give me some practice examples to work on such as the one i've worked on...
Network & Sharing
Can't find the prefix's thread for post's? (eg. [note] , [information]
Basically say's it in the title :p I seen a link before to all the prefix's for posting. Like making them move and adding in the information, note, etc
Chillout Room
Microsoft nixes '-webkit' prefix for IE Mobile for Win.
Source - http://www.zdnet.com/blog/microsoft/microsoft-nixes-webkit-prefix-for-ie-mobile-for-windows-phone-7/6173?
News
Copied link rear prefix in WLMail : wlmailhtml:{1854...
When copying a text with a link in a mail and paste it in a new mail, the link doesn't work anymore. You get an url like : wlmailhtml:{187F26DE-1F6B-4D56-A48B-26B23B16A17B}mid://00000603/!x-usc:http://www......... . Somebody an explanation herefor ? It's very annoying to remake the links...
Browsers & Mail


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 05:43.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App