Location>code7788 >text

PowerShell Quickly Changes the Names of Multiple Files

Popularity:93 ℃/2024-08-14 20:54:53

  This paper describes a system based onPowerShellLanguage, name all the files in the folder.batchMethods of replacement and modification.

  In a previous post, we described the process of creating an application based on thePythonlanguage, a way to batch modify the names of a large number of files. At the time, our need to modify file names was complex, so we chose to use thePythonlanguage to achieve this; and when our requirements renaming rules are relatively simple, they can be based on thePowerShelllanguage to realize it. In this paper, we present a program based on thePowerShelllanguage, a way to batch change the names of a large number of files in a folder.

  First, take a look at our needs. There is an existing folder, as shown below, in which we need to modify the filenames of all the files; the rule for the modification is to change the fields in the name of each of the original filesCROmodify toGRA

  Now that we know what we need, we can get started. First, in the folder where you have the files that need to be modified, press theShiftkey, while clicking the right mouse button in the blank space; subsequently, in the pop-up selection list, select "Open a Powershell window here" option, as shown below.

  Next, the window shown below will pop up.

  Subsequently, enter the code as shown below.

Get-ChildItem -Filter "*.csv" -File | ForEach-Object {
  $newname = $_.Name -replace "CRO", "GRA"
  Rename-Item $_.FullName $newname
}

  When running this command, it will use theGet-ChildItem cmdlet (command line utility) to get all the programs in the current directory that have a.csv A list of files with extensions is passed into the pipeline. Then, the pipeline symbol| Passes the passed object to theForEach-Object cmdlet, which performs the specified action on each file.

  For each file, the$newname = $_.Name -replace "CRO", "GRA" This line of code will create a new variable$newname, which contains the modified filename that will be used for all theCRO Replace withGRA$_.Name Indicates the name of the current file object.

  Finally.Rename-Item $_.FullName $newname Set the full pathname of the file ($_.FullName) with the new name ($newname) are passed together to theRename-Item cmdlet in order to rename the file to the new name.

  press a buttonenter (computer key)Press the key to run the program. Wait a few moments for the code to finish running; in which case, when the words shown below appear, it indicates that the program has finished running (in my case, because I pressed it twice at that time)enter (computer key)(key, so this field appears twice).

  At this point, you can see that the files in the folder are already renamed.

  This methodology is better than the one mentioned in the above articlePythonLanguage to modify the file name, can be relatively more convenient; especially in our file renaming needs are relatively simple, with this method can be said to be very fast and convenient.

  At this point, the job is done.