Автоматизировать скрипт | Переименовать определенный список имен файлов с помощью определенного списка слов powershell

661
Hamdoun

Весь мой код:

 #read list filenames + filter# $listname = Get-ChildItem 'Y:\Users\H\Documents\D\Game Of Throne' -Filter *.bmp   #Read list file txt# $line = Get-Content 'Y:\Users\H\Documents\D\Game Of Throne\prod\recup.txt'  #account name# $count_listname= $listname.count  #account line txt# $count_line = $line.count     #number of max action (inf_list)# $count= $count_listname, $count_line  $inf_list= ($count | measure -Min).Minimum   #Var file by file of folder# $list_split= $listname $list_split | foreach { $list_split = $_ -split '*' Write-Host $list_split [] }  #Var line by line of the textfile# $line_split = $line $line_split | foreach { $line_split = $_ -split '*' Write-Host $line_split [] } #Select type to delete# $erase=Read-Host "Enter the type to delete"  #Select number of line# $nb = Read-Host "Enter the number line to add."  #Line of replace# $list_input[$nb] = Read-Host "Line(s):"  #Boucle#  $i= 0 while ($i -le $nb-1) { $list_input[$i]| rename-item -newname { $_.name -replace ( $erase, $list_input[$nb] )} $i++ }  #output# echo "File folder" $listname "" echo "Fichier texte " $line "" echo "Number of file in folder" $count_listname "" echo "Number of line in txt" $count_line "" echo "Number max of actions" $inf_list "" echo "Line by line" $line_split "" echo "List one by one" $list_split "" echo "Type to delete" $erase ""  #echo table($line_split[n] or $line_split[n]# 

У меня есть некоторые проблемы, чтобы автоматизировать сценарий (Read-Host). Это работает с $ erase, но мне это нужно и для номера строки таблицы, а затем для строк (без текстового файла).

Hamdoun

0
Мне до сих пор не ясно, чего ты пытаешься достичь. Пожалуйста, опубликуйте содержимое списка, чтобы мы могли убедиться, что ваш подход - правильный путь. megamorf 8 лет назад 0

2 ответа на вопрос

0
megamorf

Since it's not clear what you're trying to achieve all I can show you is how to write understandable code.

Your variable names make your script hard to understand. In addition there seem to be logical flaws in it, too.

What do you need this for?

$inf_list= ($count | measure -Min).Minimum

and this:

$list_split | foreach { $list_split = $_ -split '*' Write-Host $list_split [] 

}

Where does this suddenly come from? The list_input variable hasn't been used up until that point.

$list_input[$nb]

Here's a function that if invoked like Do-Whatever -Verbose will output your images and the lines of your file (I guess that's what your split logic is for):

Function Do-Whatever { [CmdletBinding()] param( $ImagePath = 'Y:\Users\H\Documents\D\Game Of Throne', $ImageFilter = '*.bmp', $SomeList = 'Y:\Users\H\Documents\D\Game Of Throne\prod\recup.txt' ) $Images = Get-ChildItem $ImagePath -Filter $ImageFilter $ListContents = Get-Content $SomeList if ($PSBoundParameters['Verbose']) { Write-Verbose "Found Images: $($Images.count)" foreach($Image in $Images) } #[logic goes here] } 
0
Hamdoun

$inf_list:

I count the numbers of files in the folder and the number of lines in the text-file, then i take the lowest for my loop.

$list_split: I use this for make a table of all files in the folder, one by one.

$list_input: just a error don't worry

Yes it's the right logic

Actualy my code work like that :

#read list filenames + filter# $listname = Get-ChildItem 'Y:\Users\H\Documents\D\Game Of Throne' -Filter *.bmp #Read list file txt# $line = Get-Content 'Y:\Users\H\Documents\D\Game Of Throne\prod\recup.txt' #count name# $count_listname= $listname.count #count line txt# $count_line = $line.count #number of max action (inf_list)# $count= $count_listname, $count_line $inf_list= ($count | measure -Min).Minimum #count min #Var file by file of folder# $list_split= $listname $list_split | foreach { $list_split = $_ -split '*' } #Var line by line of the textfile# $line_split = $line $line_split | foreach { $line_split = $_ -split '*' Write-Host $line_split [] } #box delete# $erase=Read-Host "Put the caracter to delete" #loop# $i= 0 while ($i -le $inf_list-1) { $list_split[$i]| rename-item -newname { $_.name -replace ( $erase, $line_split[$i] )} $i++ } #output# echo "File folder" $listname "" echo "Fichier texte " $line "" echo "Number of file in folder" $count_listname "" echo "Number of line in txt" $count_line "" echo "Number max of actions" $inf_list "" echo "Line by line" $line_split "" echo "List one by one" $list_split "" echo "Type to delete" $erase " 

Then i will try with your code

Пожалуйста, используйте кнопку ** Опубликовать свой ответ ** только для фактических ответов. Вы должны [отредактировать] свой оригинальный пост, чтобы добавить дополнительную информацию. DavidPostill 8 лет назад 0

Похожие вопросы