How do I remove extension for renaming purposes? - file

I have code that looks like this
%original%-added.txt
So if the original filename is
blue.txt
My code modifies that blue.txt file and the new file is then named
blue.txt-added.txt
How can I remove that first ".txt" so that I only get
blue-added.txt

Most likely a simple REN command is all you need. I'm assuming you always want to preserve the original file extension.
ren "%original%" "?????????????????????-added.*"
Just make sure there are at least as many ? as there are characters in the original name up until the .
Here are some results you can expect
original new
-------- ------------
blue.txt blue-added.txt
part1.part2.txt part1-added.part2.txt
You could use wildcards in your source file mask. The following would append "-added" to the base name of all .txt files:
ren *.txt ?????????????????????-added.*
See How does the Windows RENAME command interpret wildcards? if you want to understand why this works.

Try like this :
%original:~0,-4%-added.txt

Related

Adjust batch file output to text file

So I´ll explain it a bit here.
I need to create a CSV file but setting the file names one by one is a pain so I was thinking of creating a bat file.
So here's what I have figured out, or actually copied and applied.
In my batch file I got the following code :
dir /b > myFiles.txt
Nice and easy it gives me the following result in the text file:
59019694038.jpg
59019694038_1.jpg
59019694038_2.jpg
59019694038_3.jpg
59019694038_4.jpg
59019694045.jpg
59019694045_1.jpg
59019694045_2.jpg
59019694045_3.jpg
bat.bat
myFiles.txt
But to make things even easier to do I'd like to add a ; after each file it finds.
So the result will be like this :
59019694038.jpg;
59019694038_1.jpg;
59019694038_2.jpg;
59019694038_3.jpg;
59019694038_4.jpg;
59019694045.jpg;
59019694045_1.jpg;
59019694045_2.jpg;
59019694045_3.jpg;
bat.bat;
myFiles.txt;
So my question was if anybody could explain me what I'm currently doing and how I could get this result.
You can replace your DIR ... with:
(FOR %%f IN (*.*) DO #ECHO %%f;)>myFiles.txt
That's a lot easier than parsing file and replacing each line.
If you don't want the line breaks, you can adapt LS_DEV's command:
(FOR %%f IN (*.*) DO #ECHO|set /P=%%f;)>myFiles.txt
Here's an alternative answer which hopefully outputs them on a single line:
#(For %%A In (*) Do #Set/P "=%%A;"<Nul)>"..\myFiles.txt"
In this case I have sent the output to myFiles.txt in the parent of the current directory, just so that it isn't included in it's own results. You can optionally change that to an alternative location or have it included in it's own content if you wish.

Changing file name while copying files in batch script

I want to copy the files from current folder to another location like (CURRENT FOLDER) file name like this (F0#CGDBANG000947532#) to another location like (\10.10.10.1\BasketsIn) with user name in the file name like (F0#CGDBANG000947532#logesh) at the end F0#CGDBANG000947532# copy to F0#CGDBANG000947532#username
thanks
Easy:
copy "c:\A" "d:\%username%_A"
EDIT
finally, after some of your comments here and in your other question, I understood your request (I think).
#echo off
for %%i in (%*) do if /i "%%~xi"==".eps" copy "%%i" "\\10.10.14.13\adman\in\displ\%%~ni%username%.%%~xi"
pause
If your file names do not have an extension or if you want the user name to be appended after the very end of the name (i.e. even after the extension), you could just use the following simple command:
COPY * \10.10.10.1\BasketsIn\*%USERNAME%
where USERNAME is a system environment variable that resolves to the user name of the current user.
If, however, you have names with extensions and you want the user name to be appended after the file name but before the file extension, you could use the ? mask character like this:
COPY * \10.10.10.1\BasketsIn\???????????????????????????????%USERNAME%.*
Just make sure you have provided enough ?s to cover the longest possible name in your case. If you are interested, you can learn more about this method in this excellent, in-depth Super User answer by dbenham:
How does the Windows RENAME command interpret wildcards?
One more note: this method may not work as expected with file names that have "multiple" extensions, i.e. like some.txt.doc.

Creating a Batch File to Rename a File

I am trying to create a batch file (filehandling.bat)
I have a file in C:\Users\username\Downloads called "hita_2013_11_05_19_11_38.csv" where the date/time changes according to the date/time it is created
I need to rename this file to "hita.csv"
The problem comes because their will always be an existing "hita.csv" file in the directory that needs to remain there unless overwritten with the above file so the REN function isn't working because it is not overwriting the file
I also need it to make no changes(do nothing) to the existing "hita.csv" file if the "hita_2013_11_05_19_11_38.csv" file doesn't exist.
I've tried the following commands and can't get any to work: REN, MOV, ROBOCOPY /MOV
I've also tried:
IF EXIST "C:\Users\username\Downloads\Hita*.csv" (
DEL C:\Users\username\Downloads\Hita.csv
REN "C:\Users\username\DownloadsHita*.csv" Hita.csv
) ELSE (
Echo The file was not found.
)
but this command still deletes the "hita.csv" file for some reason
Change your test to IF EXIST "C:\Users\username\Downloads\Hita_*.csv" (note the underscore before the '*'). The * wildcard matches 0 or more characters, so it will match Hita.csv, Hita_01.csv, or HitaXYZ.csv.
Adding the underscore makes it only match files starting with Hita_ instead.
Your hita.csv matches the wildcard search of Hita*.csv, so it will delete it.
I believe you want your wildcard search to be Hita?*.csv. The question mark should act as an "exactly one", while the asterisk is "zero or more" characters, which means Hita.csv will not match that criteria.

search and replace file names - bug fix

I have a script that changes particular string within files names (the file stores in "my_folder"):
Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("g:\my folder")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"._epf","_v0_1._epf")
if (sNewFile<>File.Name) then
File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next
the scrpit works fine if there are no folders under "g:\my folder", otherewise, if there are folders in "my folder" and the name of one (or more) of those folders are similiar to some file name, the scrip cause unwanted results like multiplying the replace string.
for example if "my folder" contain:
hello (folder)
hello_.epf (file)
then the script will eventually change the file name to:
hello_v0_1_v0_1._epf (unwanted result)
and i want the result to be:
hello_v0_1._epf
I'll appreciate quick help in this manner.
thanks.
I haven't bothered to try to figure out where your VBScript is going wrong. But you tagged your question with batch-file, batch, and batch-rename.
Here is a simple one-liner that can be run from the command prompt that will do what you want. It doesn't even need a batch script.
for %F in ("g:\my folder\*._epf") do #ren "%F" "%~nF_v0_1%~xF"
If you want to run the command within a batch script, then you need to double all percents.
#echo off
for %%F in ("g:\my folder\*._epf") do ren "%%F" "%%~nF_v0_1%%~xF"
EDIT
The above will append a new version suffix to each file name, before the extension.
If you want to replace an existing version number, then the solution is even easier. I'm assuming that your version always starts with _v, and v will never occur in the file extension.
ren "g:\my folder\*_v0_1._epf" "*v0_2.*"
The above command renames all files that end with _v0_1._epf. It preserves all characters up through the last occurance of v in the name, then adds the new version number, and finally appends the original extension.
See How does the Windows RENAME command interpret wildcards? for rules on how REN uses wildcards.

XCOPY exclude list ignored after first exclusion

I have a batch file I've created which uses xcopy to copy a dir, and child dirs, and merge them into one file. (ie. all my modulised development css stylesheets merged into one production stylesheet).
Everything is good, apart from the fact that when I reference the excludelist.txt, it only excludes the first line, and not the subsequent files I want excluded.
Anyone fancy a crack at this? You'd be most helpful.
Here's the code:
XCOPY C:\xampp\htdocs\PROJECT\css\*.* C:\temp /S /I /Y /EXCLUDE:C:\xampp\htdocs\PROJECT\exclude.txt
...and inside my exclude.txt is...
1.css
2.css
3.css
4.css
5.css
///// I know the code works (to an extent) because it is infact excluding file 1.css -- just not the ones below it. Am I right to put each exclusion on a new line?
I use the following,
xcopy "C:\users\dad\*.*" dad /s /d <yesnoyesno /EXCLUDE:excluexclu 1>cop.txt 2>err.txt
as somewhere on the web I saw a note to the effect that the /Y switch could not be used directly with an exclude file.
What I wanted to point out here, was the useful output files 1 & 2, which detail the success & failure issues.
Mine shows only success.
The short answer: Create a new text file, type the entries and save the file.
The longer explanation: I ran into this very issue on a Windows 2008 server today. I tried all kinds of things to get it to work. Forced notepad to save the file as ANSI, Unicode and UTF-8 but all of them had the same problem. Finally I started thinking about the fact that I put the file on the server via FTP and it's quite likely that FTP tweaked the file format. I created a new text file on the server, typed all the same entries in the new file and now it works.
I had a similar problem. I was trying to exclude all files with a certain extension in the root folder and any sub-folders and it didn't work. The reason was I was putting *.pdb instead of just .pdb. The newline/carriage return thing was a total red herring for me.
So my file just looked like:
.pdb
.obj
.vb
.cs
You seem to be using xcopy correctly - each exclusion file (or match depending on wildcards) should be on a new line within the file. So far I've been unable to reproduce the behaviour you're experiencing.
Is the exclude.txt listing you've given above a test file, or are they the actual css names?
What are the names of the other files that your batch file is supposed to copy?
Edit:
That the xcopy is failing to exclude further files after a single match is giving me most pause. I thought it might be to do with the type of carriage-return that was used in the exclude file, but xcopy handles unix-style carriage-returns just fine.
Can you re-verify that the correct exclude file is being used?
Try forcing it to save with ANSI encoding on your text editor.
I was having a similar issue and that did it.

Resources