0

Everyday I have to rename 6 files todaydate.94E and todaydate.94N in txt file and then I have to merge every file.

The example would be:

26/02/2020.94E

26/02/2020 (1).94E

26/02/2020 (2).94E

26/02/2020.94N

26/02/2020 (1).94N

26/02/2020 (2).94N

So, I created this batch to rename and then merge them.

if exist *.94* (
ren *.94* *.txt 
)

if not exist all.txt (
copy *.txt all.txt
) else (

del all.txt
copy *.txt all.txt
)

It work fine except that there is a conflict between the name .94E and .94N

A duplicate file name exists, or the file cannot be found.

even if I do this :

if exist *.94E (
ren *.94E *.txt 
)
if exist *.94N (
ren *.94N *.txt 
)

I saw this topic but I don't know why none of this codes work with me. For info, system is windows 10 and I am not a user admin.

Do you know why ?

2

The command ren *.94* *.txt will try to rename all matching files and change their extension to .txt.

So what happens first is that 16022020.94e is renamed to 16022020.txt.

Then, an attempt is made to rename 16022020.94n to 16022020.txt too, but that file already exists so the operation fails.

You second piece of code (after even if I do this) will fail in the same way.

Renaming all files to .txt is not possible because of the filename conflicts, but you can merge these files by simply using copy *.94? all.txt.

BTW, I assume you gave us made-up filenames (filenames on Windows can't contain a / character); if they are real, you may have other problems.

1
  • Your are right it is a space not a "/" and your solution works perfectly, thank you very much – ambou Feb 26 '20 at 14:09
1

In PowerShell, you could use:

gci -FIle | Rename-Item -NewName {$_.BaseName + $_.Extension.Substring(1) + '.txt' }
   Directory: C:\Users\keith\Sandbox\bat file


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/26/2020   1:49 PM              0 26-02-2020 (1).94E
-a----        2/26/2020   1:49 PM              0 26-02-2020 (1).94N
-a----        2/26/2020   1:49 PM              0 26-02-2020 (2).94E
-a----        2/26/2020   1:49 PM              0 26-02-2020 (2).94N
-a----        2/26/2020   1:49 PM              0 26-02-2020.94E
-a----        2/26/2020   1:49 PM              0 26-02-2020.94N


PS C:\...\bat file>gci -FIle | Rename-Item -NewName {$_.BaseName + $_.Extension.Substring(1) + '.txt' }                                                                                                     PS C:\...\bat file>gci                                                                                

    Directory: C:\Users\keith\Sandbox\bat file


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/26/2020   1:49 PM              0 26-02-2020 (1)94E.txt
-a----        2/26/2020   1:49 PM              0 26-02-2020 (1)94N.txt
-a----        2/26/2020   1:49 PM              0 26-02-2020 (2)94E.txt
-a----        2/26/2020   1:49 PM              0 26-02-2020 (2)94N.txt
-a----        2/26/2020   1:49 PM              0 26-02-202094E.txt
-a----        2/26/2020   1:49 PM              0 26-02-202094N.txt

and automate your merge as well.

3
  • ok thank you but this also rename the .ps1, I will check how to execute it in another directory – ambou Feb 27 '20 at 8:27
  • Just add a Path parameter with the path to your text files:gci -Path 'c:\Data' -FIle | Rename-Item -NewName {$_.BaseName + $_.Extension.Substring(1) + '.txt' } – Keith Miller Feb 27 '20 at 8:51
  • Or you could filter Get-CHildItem to exclude .ps1 files, but I wouldn't think it good practice to keep scripts in a data directory. – Keith Miller Feb 27 '20 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.