有任何方法/命令/软件可以执行相对文件移动吗?

有任何方法/命令/软件可以执行相对文件移动吗?

我会解释我正在尝试做什么。

我们希望从包含数千个文件的项目中移除许多符合某种模式的文件,但希望保存备份。我们正在寻找一种执行移动操作的方法,以保留目标中的相关文件夹结构。

我的意思是,如果我们有:

D:\matchingfile1.txt
D:\matchingfile2.txt
D:\nonmatchingfile1.txt
D:\nonmatchingfile2.txt
D:\foofolder\matchingfile1.txt
D:\foofolder\matchingfile2.txt
D:\foofolder\nonmatchingfile1.txt
D:\foofolder\nonmatchingfile2.txt
D:\barfolder\sub\matchingfile1.txt
D:\barfolder\sub\matchingfile2.txt
D:\barfolder\sub\nonmatchingfile1.txt
D:\barfolder\sub\nonmatchingfile2.txt

我们希望将其移动到 D:\_BACKUP\20130527\,结果如下:

D:\_BACKUP\20130527\matchingfile1.txt
D:\_BACKUP\20130527\matchingfile2.txt
D:\_BACKUP\20130527\foofolder\matchingfile1.txt
D:\_BACKUP\20130527\foofolder\matchingfile2.txt
D:\_BACKUP\20130527\barfolder\sub\matchingfile1.txt
D:\_BACKUP\20130527\barfolder\sub\matchingfile2.txt

注1:要移动的文件不是名为“matchingfile”的文件,这只是一个说明性的例子。我们目前提取的是目标文件的所有完整路径的列表(纯文本),因此这必须是方法/命令/程序的输入。

注2:目录级别可以是任意的。

工作在Windows 7操作系统下。

提前致谢。

答案1

我回到家后看了看,这对我有用

setlocal EnableDelayedExpansion

IF [%1]==[] (set txtpath=%CD%\list.txt) else (set txtpath=%1)
set projectfolder="D:\"
set savelocation="D:\_Backup"

cd /d %projectfolder%
set lenght=%CD%
set i=-1
set n=0
:nextChar
    set /A i+=1
    set c=!lenght:~%i%,1!
    set /A n+=1
    if "!c!" == "" goto endLine 
    goto nextChar

:endLine
for /f "tokens=*" %%A in (!txtpath!) do call :filecheck "%%A"
goto :eof

:filecheck
set folder=%~pd1%
set location="!folder:~%n%!"
if not exist %savelocation%\%location% mkdir %savelocation%\%location%
copy %1 %savelocation%\%location% && del /q %1
goto :eof
endlocal

重新制作脚本以反映您想要一个 *.txt 文件作为文件路径的输入,这对我来说有效,您需要设置“projectfolder”、“savelocation”、“txtpath”,但之后脚本可以从任何地方运行,并执行您想要的操作。(您可以在设置 projectfolder/savelocation 后将 txt 文件拖放到其上)

它会在驱动器(或其他驱动器)的任何位置重新创建 .txt 文件中的所有文件的文件夹结构,复制文件,然后将其从原始文件夹中删除。

答案2

嗯,这个是一个简单的 xcopy 命令,但是你在 D: 上有 _backup 文件夹,因此遍历 D: 驱动器将会获取该_backup文件夹

你可以隐藏它_backup,这样你就可以

xcopy d:\matchingile?.txt d:\_backup\matchingfile?.txt /s

答案3

就我个人而言,我刚刚开始学习一些 powershell 脚本,这对于这样的工作来说非常有用。只需将代码保存在 .ps1 文件中,并确保启用 powershell 脚本的运行 (Set-ExecutionPolicy RemoteSigned)。这样,你就有了一个更强大的过滤工​​具,使用正则表达式,而不仅仅是通配符。当然,也可以修改脚本以接受来自文本文件的文件列表。

# Variables
$backupFolder="D:\_BACKUP\20130527";
$folderTobeBackedUp="D:\";
$overwrite=$True;
$filter="filenameToBeMatched";

function recurseDir($dir)
{
    $dirs=$(get-childitem "$($dir.FullName)");

    foreach( $f in $dirs)
    {
        if( $f.PSisContainer )
        {
            recurseDir $f;
        }    
        elseif($f.Name -imatch "$filter")
        {
            copyFile $f;
        }
    }
}

function copyFile($f)
{
    $newFile=$($f.FullName).Replace($folderTobeBackedUp, $backupFolder);
    $parent="$(Split-Path $newFile -Parent)";
    if( -not (test-path $parent))
    {
        new-item -Path $parent -type "directory";
    }
    try
    {
        $f.CopyTo("$newFile", $overwrite);
        (rmdir -Path $f.FullName -Force);
    }
    catch
    {
        Write-Host $_.Exception.ToString();
    }   
}

$folders=$(get-childitem "$folderTobeBackedUp");
foreach($f in $folders)
{
    if( $f.Name -imatch "_BACKUP" )
    {
        ; # Do nothing.
    }
    elseif( $f.PSisContainer )
    {
        recurseDir $f;
    }
    elseif($f.Name -imatch "$filter")
    {
        copyFile $f;
    }
}

答案4

XCOPY 可以完成您需要的工作。它可按如下方式用于批处理作业:

xcopy "C:\FolderName\*.*" "D:\FolderName\*.*" /D /E /V /C /I /F /H /R /K /Y /Z

以下是完整的语法,包括开关的含义:

C:\>xcopy /?
Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
                           [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
                           [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z]
                           [/EXCLUDE:file1[+file2][+file3]...]

  source       Specifies the file(s) to copy.
  destination  Specifies the location and/or name of new files.
  /A           Copies only files with the archive attribute set,
               doesn't change the attribute.
  /M           Copies only files with the archive attribute set,
               turns off the archive attribute.
  /D:m-d-y     Copies files changed on or after the specified date.
               If no date is given, copies only those files whose
               source time is newer than the destination time.
  /EXCLUDE:file1[+file2][+file3]...
               Specifies a list of files containing strings.  Each string
               should be in a separate line in the files.  When any of the
               strings match any part of the absolute path of the file to be
               copied, that file will be excluded from being copied.  For
               example, specifying a string like \obj\ or .obj will exclude
               all files underneath the directory obj or all files with the
               .obj extension respectively.
  /P           Prompts you before creating each destination file.
  /S           Copies directories and subdirectories except empty ones.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /V           Verifies each new file.
  /W           Prompts you to press a key before copying.
  /C           Continues copying even if errors occur.
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Q           Does not display file names while copying.
  /F           Displays full source and destination file names while copying.
  /L           Displays files that would be copied.
  /G           Allows the copying of encrypted files to destination that does
               not support encryption.
  /H           Copies hidden and system files also.
  /R           Overwrites read-only files.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
               empty directories and subdirectories.
  /U           Copies only files that already exist in destination.
  /K           Copies attributes. Normal Xcopy will reset read-only attributes.
  /N           Copies using the generated short names.
  /O           Copies file ownership and ACL information.
  /X           Copies file audit settings (implies /O).
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

相关内容