issue with combining csv in batch - batch-file

I'm using this script to combine multiple csv files all with the same header and all in the same directory. It works well, but it writes the file path to the last line in MASTER.csv, which is an issue when I go to process the data. I'd like to append this batch file so that it no longer outputs the file path, otherwise how would I delete the last line in the MASTER.csv
#echo off
setlocal EnableDelayedExpansion
set first=1
set fileName="MASTER.csv"
>%fileName% (
for %%F in (*.csv) do (
if not "%%F"==%fileName% (
if defined first (
type "%%F"
set "first="
) else more +1 "%%F"
)
)
)
Example of the last three lines of MASTER.csv:
2014/05/31,23:58:00, 22.0C, 22.33C, 16.17,OK,OK,OK,OK,OK,OK,OK,OK,OK,Off
2014/05/31,23:59:00, 21.9C, 22.39C, 16.11,OK,OK,OK,OK,OK,OK,OK,OK,OK,Off
C:\Users\ME\Desktop\program\data\enviro\MASTER.csv

file.bat | find /v "C:\Users\ME\Desktop\program\data\enviro\MASTER.csv" > MASTER.csv

Related

Batch file to Move files based on part of filename, to folder based on part of folder name

Found a pyhton solution here, but I need a batch file-based solution.
Have many files:
SSP4325_blah-blah-blah.xml
JKP7645_blah.xml
YTG6457-blah-blah.xml
And folder names that contain a piece of the file name:
RefID - SSP4325, JKP7645, GHT1278, YRR0023
RefID - YTG6457
I'm looking for a batch solution which would read a portion of the file name at the front (before either the first dash or underscore) and then move that file into the folder where the front of the filename exists as part of the folder name.
So in the above examples, the first two files (SSP4325 and JKP7645) were moved into the first folder because it contained it contained that text as part of the folder name.
The third file would be moved into the second folder.
I have hundreds of files and 63 folders. So I'm hoping to be able to automate.
Can't use Powershell or Python due to limitations of the environment. So hoping for a batch file approach.
Thanks. Sean.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.xml" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%\*%%b*" '
) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
After establishing the directories, the outer loop puts the filename in %%a, the next loop gets the first part of that name, up to but not including the first - or _ (the delims specified) into %%b.
The inner loop finds the target directory containng %%b in the destination directory and constructs an appropriate move line.
This solution review the folders just one time and store they in an array, so this method should run faster.
#echo off
setlocal EnableDelayedExpansion
rem Process the folders
set i=0
for /D %%a in (*) do (
rem Store this folder in the next array element
set /A i+=1
set "folder[!i!]=%%a"
rem Separate folder in parts and store the number of the array element in each one
for %%b in (%%a) do set "part[%%b]=!i!"
)
rem Process the files
for %%a in (*.xml) do (
rem Get the first part of name
for /F "delims=-_" %%b in ("%%a") do (
rem If such a folder exists...
if defined part[%%b] (
rem Get the number of the corresponding array element and move the file
for %%n in (!part[%%b]!) do ECHO move "%%a" "!folder[%%n]!"
) else (
echo No folder exists for this file: "%%a"
)
)
)
This method have also several advantages: you may check if a certain folder does not exists, or get the number of files moved to each folder, etc. If you are not interested in these points, just remove the if command and make the code simpler...
An explanation of array management in Batch files is given at this answer.

How can i properly make this kind of loop?

I have 2 txt files, first file is "recipes.txt", second is "sites.txt" where is website links,
i'm making txt files in a bulk with the names from file "recipes.txt", from second txt file "sites.txt" i want to take rows in a loop and input them into txt file which gonna be created from recipes.txt.
Here i have batch script, which creates files with specific name from "recipes.txt", but its appending only last row of the file "sites.txt".
What shoudl i change to amke it work properly?
#echo off
setlocal enableDelayedExpansion
for /f %%g in (recipes.txt) do (
for /f %%i in (site.txt) do (
(
echo SET !REPLAYSPEED MEDIUM
echo URL GOTO=%%i
)
)>C:\Users\Viktor\Desktop\Sites\Scripts\%%g".iim"
)
pause >nul
just extend your block for redirection to include the for itself:
for /f %%g in (recipes.txt) do (
(
for /f %%i in (site.txt) do (
echo SET !REPLAYSPEED MEDIUM
echo URL GOTO=%%i
)
)>C:\Users\Viktor\Desktop\Sites\Scripts\%%g".iim"
)
pause >nul
Your version overwrites your file with every iteration of %%i

Windows Batch script to get substring of filenames in a folder

I would like to loop through a folder and obtain only the last 13 characters of the file names. Part of what I did is as follows:
for /r . %%g in (*.pdf) do (
set var=%%~g
echo %var:~-13%
)
The problem I have is that it is only printing one file name for all the files in the folder. E.g I have the following file names as input :
ARC_1384343086954570285.pdf,
ARC_1384343288950263728.pdf,
ARC_1384343297950370887.pdf.
The output comes out as :
950370887.pdf
950370887.pdf
950370887.pdf
Any time you set a variable inside of a for loop, you have to use delayedexpansion to access it.
setlocal enabledelayedexpansion
for /r . %%g in (*.pdf) do (
set var=%%~g
echo !var:~-13!
)

Windows Batch File to Read textfile and append for pdftk

I have a text file which contains the location of a list of pdf files. I am writing a windows batch file that needs to read this line by line and append in a command which will be executed to merge all the pdfs into 1 pdf using pdftk.
Below is the example command:
pdftk "C:\test\1.pdf" "C:\test\2.pdf"......"C:\test\50.pdf" cat output merged.pdf
I came across this How do you loop through each line in a text file using a windows batch file? for reading text file.
But how do I read and append to a variable which can then be used for the command mentioned above?
Assuming your list of pdf files looks like this
pdf1.pdf
pdf2.pdf
pdf3.pdf
Then you can use this to concatenate them into one variable
setlocal enabledelayedexpansion
set files=
for /f "tokens=*" %%a in (pdfs.txt) do (
if defined files (
set files=!files! "%%a"
) else (
set files="%%a"
)
)
pdftk !files! cat output merged.pdf
The if else is there to remove the leading space from the variable, I wasn't sure if that would make a difference. If it doesn't then you can get rid of it and just use
setlocal enabledelayedexpansion
set files=
for /f "tokens=*" %%a in (pdfs.txt) do (
set files=!files! "%%a"
)
pdftk !files! cat output merged.pdf

How to rename and move files to new directory

I would like to rename files that are uploaded to another server from extension .txt to .txt_mvd and move to a different directory for archiving in a Windows batch mode. Can anyone help with what the windows batch script should be?
Thanks.
Here is the code
FOR /R C:\your_folder %%d IN (*.txt) DO (
ren %%d %%~nd.txt_mvd
)
%%d is the full file name + path
%%~nd return only the file name without the extension
Using the /R parameter, it will scan folder and subfolder
UPDATE 1
The following code should work as required.
I've added an IF that ignore the subfolders.
FOR /R E:\your_folder\ %%d IN (*.*) DO (
IF %%~dpd==E:\your_folder\ (
ren %%d %%~nd.txt_mvd
)
)
UPDATE 2
Fixed code
FOR /R E:\your_folder\ %%d IN (*.txt) DO (
IF %%~dpd==E:\your_folder\ (
ren %%d %%~nd.txt_mvd
)
)
UPDATE 3
Here is a more generalized and parametrized version of the script.
Change the starting parameter to your need (the first 4 lines of code).
This script first rename the files you choose (1st parameter) in your starting folder (3rd parameter), change the extension to the new one (2nd parameter), and then move the renamed files in the folder of your choice (4th parameter).
set Extension_of_file_you_want_to_renamne_and_move=txt
set New_extension_of_moved_files=txt_mvd
set Folder_that_contain_your_files=C:\Your_starting_folder\
set Folder_where_to_move_your_files=C:\Your_destnation_folder\
FOR /R %Folder_that_contain_your_files% %%d IN (*.%Extension_of_file_you_want_to_renamne_and_move%) DO (
IF %%~dpd==%Folder_that_contain_your_files% (
IF %%~xd==.%Extension_of_file_you_want_to_renamne_and_move% (
ren "%%~d" "%%~nd.%New_extension_of_moved_files%"
move "%%~dpnd.%New_extension_of_moved_files%" "%Folder_where_to_move_your_files%"
)
)
)
when you change the parameter DON'T add any space.
So DON'T change the parameter like that:
set Folder_that_contain_your_files = c:\myFolder <--- WRONG, WON'T WORK, there are unneeded space
instead, write the parameter WITHOUT unneeded space:
set Folder_that_contain_your_files=c:\myFolder <--- OK, THIS WILL WORK, there are no extra spaces
UPDATE 4
Fixed the code, I've added some quotation marks, without them the code wont works if folder name contained spaces.
set Extension_of_file_you_want_to_renamne_and_move=txt
set New_extension_of_moved_files=txt_mvd
set Folder_that_contain_your_files=C:\Your_starting_folder\
set Folder_where_to_move_your_files=C:\Your_destnation_folder\
FOR /R "%Folder_that_contain_your_files%" %%d IN (*.%Extension_of_file_you_want_to_renamne_and_move%) DO (
IF "%%~dpd"=="%Folder_that_contain_your_files%" (
IF %%~xd==.%Extension_of_file_you_want_to_renamne_and_move% (
ren "%%~d" "%%~nd.%New_extension_of_moved_files%"
move "%%~dpnd.%New_extension_of_moved_files%" "%Folder_where_to_move_your_files%"
)
)
)

Resources