创建将执行以下操作的.bat文件

时间:2018-09-26 04:11:10

标签: batch-file

我需要帮助来创建.bat文件,该文件将要求用户在具有数千个.txt文件的特定文件位置中输入指定的文件名搜索,将单个文件移至用户桌面,并将其.txt扩展名更改为.csv。如果可能,循环代码而不关闭/退出.bat。任何形式的帮助都是值得的。谢谢。

1 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow。在这里,我们不仅会打印免费代码供您使用。但是由于您说过您不知道如何编码,所以我为您制作了文件,并提供了每个命令的作用步骤。

我编写的代码不起作用,因为我必须导入一个不能完全批量工作的“选择文件夹”模块,因此最终文件可以作为可执行文件下载merge_asof

这是源代码,如果您想了解其工作原理,应查看一下源代码:

@echo off

rem <--- This is the REM command. Anything typed after "rem" will not show up. It is used mainly to comment.

rem The first line of code was "@echo off". Echo is the command used to manage printing text. In this case, we made it so anything typed will not show us other random text.

cls

rem "cls" is the command used to clear your screen of text and colors. I put it at the top in case you invoke the file from the command prompt, it will clear your screen.

color 9

rem "Color" is the command you write to flavor your text. The color light blue looks nice, so the code "9" will set all the text to that color.
rem Here's a list of all the different colors and their corresponding codes:
rem     0 = Black       8 = Gray
rem     1 = Blue        9 = Light Blue
rem     2 = Green       A = Light Green
rem     3 = Aqua        B = Light Aqua
rem     4 = Red         C = Light Red
rem     5 = Purple      D = Light Purple
rem     6 = Yellow      E = Light Yellow
rem     7 = White       F = Bright White
rem The command itself works like this: color <backgroundColor><foregroundColor>
rem So if I type "color F0", the background will be Bright White and the Text will be Black

echo;

rem "echo;" in this instance will produce a new blank line. I put it there just so it will look nicer once we run the file.

echo;
echo From which directory to produce tranfers?

rem Woah! This "echo" command is helpful! In THIS instance, we're using it to print text. Anything written after "echo" will be printed on screen.

:Repeat

rem ":Repeat" is a label. This means that at any point in the code we can go back to it and continue executing commands from where it started. You can make a label by typing ":" with a name after it.

cls
echo Selected Folder: %dir%

echo;
echo;
echo Which file to transfer? (Don't write extention)
set /P filetoget= 

rem Cool! Finally! User input! "Set" is the command used for managing variables. In this instance, we are allowing the newly created variable "filetoget" to be set to whatever the user types after the "=" sign.

echo;
echo;
echo Locating...

rem "Locating..." will now be printed on screen. I put a couple of other notices to keep you updated on where the code is currently running. This is also helpful for when you get an error message, then you will know around what point in the code your error is taking place.

cd %dir%

rem "CD" keeps track of where the command interpreter should be directory-wise. This means that the command interpreter will check for files, create files, read files, manage directories and so much more based on where the "CD" is currently pointed in. For example, having the "CD" set to your folder means now we can move a file from that folder to wherever we want.

IF NOT EXIST %filetoget%.txt goto NotFound

rem "IF"! The lovely "IF"! So many uses, I can't list them all. But for the most part, you're right. It detects if "IF" statements are true or false. "IF EXIST" or "IF NOT EXIST" makes sure that a file is existing at the location of the current "CD".
rem "Goto" is our twin for the Labels we talked about earlier. This command in total says "Check if the file that the user told us about earlier exists. If it doesn't, go to a place in the code labeled ':NotFound'"

echo Moving...
move /Y %filetoget%.txt "%UserProfile%\Desktop"

rem Here is the actual fun bit. Doing the process of moving the file. "Move" moves files from one place to another. Here, we are moving the variable the user gave us earlier (the file we selected) to the Desktop. Also, the "/Y" means that if there is another file in the same folder with the same name we are about to set it to, it will override it and replace it.
cd "%UserProfile%\Desktop"

echo Changing filetype...
rename /Y %filetoget%.txt %filetoget%.csv

rem "Rename" is just as simple to use as "move". Here we take the file the user gave us, and change the .txt extension to a .csv extension. We could also change the name of the file itself, but in this case we don't want to.
echo Completed!
timeout /T 0 /NOBREAK >nul

rem "Timeout" is simple but hard to understand at a first glance. It pauses the interface for an amount of time, in this case for 0 seconds. 0 seconds on a "timeout" command stops it for just the slightest bit. I put this here so you can perhaps notice the "Completed" message I wrote before it. Obviously you could set a bigger number for more seconds.

goto Repeat

rem Mainly, here our code ends. Since all was completed successfully, it should go back to the Label "Repeat" to loop all the commands we have just executed.

:NotFound
echo;
echo;
echo Your file was not found in the directory selected! Make sure you have typed the name of the file without it's extension, and that you have selected the correct directory.
echo;
pause

rem "Pause" is the command used to pause the command prompt until a key is pressed. Pretty simple.

goto Repeat