How to copy files throuth a windows command line? - command-line

I want to copy many text files from one folder to another. The file names are contained in another text file. So the commands should be able to read in the file names and do the copy things. I can do this with R but it's very slow. I wonder if it's possible to do this with the command line? (I can copy single file with the command line, but don't know how to copy many with for or while loop or something.) Thanks in advance.

I found this question helpful: How do you loop through each line in a text file using a windows batch file?
This is what you need to just paste into the command line. If you want to save it in a bash file you need to use %% instead of % for variables.
for /F "tokens=*" %a in (myfile.txt) do copy "%a" "new folder\%a"
This simply loops through the file, and for each line does a copy of it to the new folder. The quotes are important in case of spaces in the filenames.

Related

AHK Run command with directory path containing space

Using AHK script to open up and launch text files (or script files) within notepad++. I recently had to add spaces to my file path which has caused the problems I now experience. It's as if the space in the file path is escaping the command.
e.g.
Run % "notepad++.exe C:\C Docs\SW\AHK\Desktop1.ahk"
Upon running the above line, it will ask in msgbox: "C:\C" doesn't exist. Create it?
This script happens to be the script location itself. So I also tried the following without success (produces same message):
Run % "notepad++.exe " . a_scriptdir . "\" . A_ScriptName
You are passing two arguments to Notepad++ the first one being C:\C and the second one being Docs\SW\AHK\Desktop1.ahk.
To pass them as one argument, do what you'd always do with command line arguments, quote them.
Run, % "notepad++.exe ""C:\C Docs\SW\AHK\Desktop1.ahk"""
Try like this:
Run notepad++.exe "C:\C Docs\SW\AHK\Desktop1.ahk"

Rename and overwrite files using wildcard in Windows

I am working on a script for auto deployment, where I need to replace my files with the same filenames.
For example, I have the following files in my current directory
deployment.properties
wrapper.conf
config.properties
Later, I will generate another set of files like this
deployment.properties.tokenized
wrapper.conf.tokenized
config.properties.tokenized
Lastly, I want to replace the existing config files (in the first code block) using the *.tokenized version and remove the tokenized files.
In Linux, the following can do the job. But I don't know how to do in Windows
for f in *tokenized;
do mv "$f" "`echo $f | sed s/tokenized//`";
done
I tried to use powershell's move-item, rename-item but still cannot figure out the right way to do it. Could somebody help? bat / powershell scripts are both welcomed. Using loop is also okay. Thank you.
It is almost the same code but in cmd / batch files we have access to the elements of the file name.
From command line
for %a in (*.tokenized) do move /y "%a" "%~na"
Or, for a batch file (you need to escape the for replaceable parameter)
for %%a in (*.tokenized) do move /y "%%a" "%%~na"
As the extension of the file (the text after the last dot) is .tokenized, when you request just the name (without extension) of the file being referenced (%%~na) you get the original file name.
This PowerShell script should do the job:
Get-ChildItem *.tokenized | % {
move $_.Name ([System.IO.Path]::GetFileNameWithoutExtension($_.Name)) -Force
}

Merge txt file: TXTcollector

I am looking for an easy way (without windows commands) to merge several txt files.
Does anyone has an advice about the software TXTCollector? Is it a good one?
Thanks
A command prompt or .BAT/.CMD file can accomplish this easily by redirecting the output to a file.
type first.txt > merge.txt
type second.txt >> merge.txt
type third.txt >> merge.txt
A single redirection operator (e.g. >) will always create a new file with the output, overwriting any existing target file. Double redirection operators (e.g. >>) will append the output to an existing file or create a new file if one does not exist.
In the above .CMD script, the contents of first.txt, second.txt and third.txt will be in the new merge.txt.
EDIT Sample .CMD files
In its simplest form, a .CMD script that loops through the .txt files in a folder would look something like this.
#echo OFF
FOR %%c in (*.txt) DO TYPE %%c >> merge.txt
There is plenty of examples available on the web if you need to make adjustments for your particular situation.

finding a files path in the command line

I am doing a batch scripting assignment where I have to call one script from inside another. I need the script to run the second script no matter where my lecturer saves these scripts. How would I do this. Is there some way to find the path of script inside the script and use that to execute the file. Any help would be great. I think I need to use %'s but i'm not sure.
The name of the script is Hello World.bat.
How would I copy Hello World.bat to the C:\ if I don't know which directory the lecturer has placed it in. what command/s would I use so that the copy would work regardless of the scripts location.
I don't see the "DOS" tag, but I'll assume that it is for now. If you want the entire path, you can get it by doing this:
echo %cd%
If you want just the last folder, this works (inside a .bat file):
for %%* in (.) do #echo %%~n*
Note that from the command line, the above command will work with single %'s:
for %* in (.) do #echo %~n*
If the script you are executing is calling other scripts in the SAME folder location, you can prefix the path statement with "%~dp0" or "%~dps0" but do not put a backslash between that and the name of the script you are calling. In other words, if script1.bat is calling script2.bat in the same folder, the statement in script1.bat would refer to "%~dp0script2.bat"
sorry about batch files, am not familiar, but in nix shell, there is the locate command which can return the path of the file , if you know the filename exactly and the name is unique.
like
name=$(locate filname)

matlab, textfile

I have a bunch of text files which have both strings and numbers in it, but the string are just in the first few rows.
I'm trying to write a script which go in to my folder search all the file in the folder and delete the text from the files and write the rest as it is in the new text file.
Does anybody know how?
I don't think this is a good use of MATLAB.
I think you'd be better off scripting this in Python or shell. Here is one way you could do it with tr in shell if you're on *nix or mac and if your files are all in the same directory and all have the file extension .txt:
#!/bin/sh
for i in `ls *.txt`
do
cat $i | tr -d "[:alpha:]" > $i.tr.txt
done
To run. save the code above as a file, make it executable (chmod a+x filename), and run it in the directory with your text files.
If the number of string lines is always the same, you can use textread() with 'headerlines' option to skip over those string lines, then write the entire text buffer out.