Reputation: 87
I have thousands of text files formatted something similar to abcdefgh_20140430120000.txt
I would like to copy those from one folder to another folder like
xcopy "Y:\FolderA" "C:\FolderB"
However, I want to remove the time portion in the file name so that the text file would look like abcdefgh_20140430.txt
Can anyone help me with code to do so?
Upvotes: 2
Views: 47393
Reputation: 11
COPY "C:PathOfFileToCopy\Filename" "C:PathOfCopyDestination\NewFilename"
RENAME "C:PathOfFileToCopy\Filename" NewFileName
Upvotes: -1
Reputation: 41234
Launch this in the folder: it should copy them as you wish and remove the last 6 characters of the filename.
Filenames with !
in them will generate an error.
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir *.txt /b /a-d ') do (
set "name=%%~na"
copy "%%a" "C:\FolderB\!name:~0,-6!%%~xa"
)
Upvotes: 4
Reputation: 186
You could use the rename command to rid of the unwanted parts of the copied file name.
copy "Y:\FolderA" "C:\FolderB"
rename "C:\FolderB" "C:\FolderC"
That should work, if you have any problems then tell me and I'll try my best to fix it :)
You also do not need the x in front of copy, or at least I didn't need it since I'm using Windows 8.1, if that has anything to do with it.
Upvotes: 3