Вопрос

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?

Это было полезно?

Решение

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" 
)

Другие советы

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.

COPY "C:PathOfFileToCopy\Filename" "C:PathOfCopyDestination\NewFilename"

RENAME "C:PathOfFileToCopy\Filename" NewFileName
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top