Automate script | Rename a defined list of filename by a defined list of word powershell

0

1

All my code:

    #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]#

I have some problems to automate the script (Read-Host) This is working with the $erase but i need it too for the number of table-line and then the lines (without a textfile)

Hamdoun

Hamdoun

Posted 2015-06-12T14:44:07.577

Reputation: 21

To me it's still not clear what you're trying to achieve. Please post the list contents so that we can see if your approach is the proper way to go. – megamorf – 2015-06-15T08:41:57.167

Answers

0

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) {Write-Verbose $Image.Fullname)

        Write-Verbose "List entries count: $($ListContents.count)"
        foreach($Line in $ListContents) { Write-Verbose $Line}
    }

    #[logic goes here]
}

megamorf

Posted 2015-06-12T14:44:07.577

Reputation: 1 494

0

$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

Hamdoun

Posted 2015-06-12T14:44:07.577

Reputation: 21

Please use the Post Your Answer button only for actual answers. You should [edit] your original post to add additional information. – DavidPostill – 2015-06-20T00:37:01.097