Как написать код для перемещения файлов Excel на основе FileName? - PullRequest
0 голосов
/ 02 мая 2019

Я бы хотел переместить файлы Excel, основанные на имени файла на сервере, из одной папки в другую, используя Powershell.Например:

\Desktop\SourceTestARC\ to \Desktop\DestinationTestARC\14_file,
\Desktop\SourceTestARC\ to \Desktop\DestinationTestARC\51_file, 
\Desktop\SourceTestARC\ to \Desktop\DestinationTestARC\55_file, etc.

Скрипт должен прочитать первые две цифры файла Excel и переместить его в соответствующую папку.

Например:

1420193344.dat goes to 14_file, 
51201997748.dat goes to 51_file.

Ответы [ 2 ]

0 голосов
/ 02 мая 2019

Вот возможное решение:

# Set the Path of the SourceFiles
$SourcePath = "C:\Users\Packard-User\Desktop\Neuer Ordner\"

# get all items of the folder
$FilesInFolder = Get-ChildItem -Path $SourcePath



foreach ($SourceFile in $FilesInFolder)
{

    # i dont like complex statements, because of that a use a helper variable
    $Helper1 = $SourceFile.Name

    # create the destination folder variable, take two characters from the beginning (0), adding the "\" is necessary to show it is a folder
    $DestinationFolder = $SourcePath+$Helper1.Substring(0,2)+"\"

    
    # create the destination folder, throws an exception if the folder already exists
    New-Item -Path $DestinationFolder -ItemType Directory

    # move the item to the destinationfolder
    Copy-Item -Path $SourceFile.FullName -Destination $DestinationFolder
    
    
}
0 голосов
/ 02 мая 2019

Get-Childitems исходной папки, затем создайте цикл foreach.

Сохраните имя файла в переменной, затем используйте оператор $ Variable.Substring для получения необходимых двух цифр, создайте переменную (например, $ DestinationFolder), в которой вы объединяете исходную папку с переменной двух цифр. Затем с помощью командлета Copy-Item скопируйте файл в новое место назначения, а для параметра -destination используйте переменную $ DestinationFolder.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...