how to copy a file name using cmd and rename another using copied text (Clipboard) only with cmd windows commands

Asked

Viewed 879 times

0

Hello. I am looking forward to facing a situation at Windows 10 CMD command prompt of "dynamic name", so to speak.

SITUATION EXAMPLE:

I have in my directory the following file: .../DADOS/teste.csv (whereas of all the files in that directory, this is the only one with the extension .csv)

My intention is to create any new file, but with this same name in any other directory. However, I need you to accomplish this task by using cmd commands so I can put it in a beginner script.

My initial solution was the following:

First I needed to copy the file name. I’m sure there must be an easier command to copy the file name. But I did it through the sequence of commands:

(1) .../DADOS>dir /b *csv > nome.txt . This command creates a txt file with the name of the file I want ('test.csv').

this can be viewed with the command ...DADOS>type nome.txt that will display test.csv (the only line written in the file 'name.txt')

(2) I copied the text written in the pro file (clipboard) with the CLIP command:

.../DADOS>clip < nome.txt

Here, if I hit CTRL+V or (for paste option) in any editor I can prove that I copied the text 'name.txt'.

Ready! I have the text I want in the clipboard.

Now what I need is; if I create any file in another directory, for example...

.../dados2>echo > novo_arquivo.txt

my goal is to rename this new file with the text in "Clipboard" using a cmd command line, do not use CTRL+V or mouse button.

I need this

.../dados2/novo_arquivo.txt --rename--> .../dados2/teste.csv

would be something like

>rename nome_original novo_nome=clipboard or >rename novo_arquivo.txt novo_nome_clipboard

(but I need a command that actually worked on the windows cmd).

NOTE: Notice that I used the term "dynamic name" at the beginning, because in this case, the name of the new file would always be changing, according to the name of the initial file. I thought it would be simple to create a sequence of commands that copies the name of the first file and in front, when I needed, use this name (that would be in the Clipboard) to rename a new file.

Can someone help me? How to create a file and rename it with the text of the clipboard, or already create a file with the name copied in the Clipboard. Or you can make that nomenclature reference.

1 answer

0

You are literally counting the oxen by the paws

It would be simpler to take the Clipboard out of the process or use it only at the end:


1. List your arquivo.csv most recently created/amended:

dir /o:-d /tc /a:a /b ..\DADOS\*.csv

2. Use this output to create your file (%%~ni) in ..\DADOS2:

for /f tokens^=* %%i in ('dir /o:-d /tc /a:a /b *.csv')do cd.>"..\DADOS2%%~ni.txt"

  • On the command line:
cd /d "C:\caminho\completo\para\DADOS" && for /f tokens^=* %i in ('dir /o:-d /tc /a:a /b *.csv')do cd.>"C:\caminho\completo\para\DADOS2\%~ni.txt"
  • In your bat/cmd:
@echo off 

cd /d "C:\Caminho\Para\Pasta\DADOS"

for /f tokens^=* %%i in ('dir /o:-d /tc /a:a /b *.csv
')do cd.>"D:\Caminho\Para\DADOS2\%%~ni.txt"

Obs.: If necessary: .. I can prove that I copied the text 'name.txt'*:

@echo off

cd /d "C:\Caminho\Para\Pasta\DADOS"
set "_dados2=C:\Caminho\Para\Pasta\DADOS2"

for /f "tokens=*" %%i in ('dir /o:-d /tc /a:a /b *.csv
')do echo;%%~ni.csv|clip && ;cd.>"%_dados2%\%%~ni.txt"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.