PowerShell move file to folder with same name

move-item where same name files exist

I have multiple folders where the files are renamed by a value that could result in the same name. I could for example end up trying to move file a.txt into a folder with a.txt already present. Currently I have a means to detect if file a.txt exists, then I use an index value for the new file. The new a.txt, becomes a_1.txt.

The question:is there a better way? For example - could tray-catch be employed? Since I could have thousands of files to transfer All naming is based on number series followed by the index and file names. For example 123-23-aardvark.txt, 123-24-aardvard.txt. Where the array values are $x[0]=123, $x[1]=23, $x[2]=aardvark.txt, as so on.

Current I detect the last file with a given name, split it and index x[1] - the index number -is incremented by 1. The name is recombined as the new file name. The first and last array values must be unchanged in the file name because of the search used to locate them later in the process.

Just curious?

  • Reply
  • Cancel
  • Cancel