|
Answer» Hello people
I got a little question here that shouldn't be very long.
What I'm trying to do, basically, is creating a .csv file using only a single .bat program. That would help my department a lot because .csv file are very long to CREATE (by human) and generating them automatically would help us out a lot!
Here's what i currently have
------------------------------------------------------
@ECHO OFF CLS REM This program will generate a .csv file for import. @ECHO OFF Will hide the command flow because no one cares. SET T=,4, SET C=, ECHO ....................................... .......................... ECHO Welcome to the csv generating tool. Press enter to continue. ECHO ....................................... .......................... PAUSE > nul ECHO Make sure this program and all the files are in a single directory. ECHO It'll take the files and file path to generate a .csv to import. ECHO Once this operation is done, zip all the files together. SET /P D=Please Input the ID Number: PAUSE > nul ECHO Attachment Name,Type,File,Related Event > Output.csv
--------------------------------------------------------
Now, for every file inside the folder where the program will be dropped, i need to generate a text line with these information:
For attachment name, i simply need the file name (like Report.pdf or Audio.mp4) For type, i need 4 (therefore %T%) For File, i need the filepath from inside the .zip. That means i need to "trim" the filepath from :
C:\Documents and Settings\Username\Desktop\Test File\test1\txt1.txt
to
\test1\txt1.txt
and for related event, that would be the user input here.
Therefore a line would look like :
%ATTACHMENTNAME%%T%%TRIMMEDFILEPATH%C%%D% (%C% is only a single comma)
Translated it would be like this (let's say user input is 999)
txt1.txt,4,\test1\txt1.txt,999
And this for all files contained in within the folder. Final input in notepad would look like :
Attachment Name,Type,File,Related Event txt1.txt,4,\test1\txt1.txt,999 txt2.txt,4,\test1\txt2.txt,999 txt3.txt,4,\test1\txt3.txt,999 txt4.txt,4,\test2\txt4.txt,999
And so on.
I am willing to use other programming language if needed but i believe batch should work. As long as it can run with a single executable with a double click that would be perfect (i don't want to confuse our poor secretary).
For those wondering, yeah, that's my first batch program. :p
Thanks in advance please help My suggestion would be to do this in PERL instead of batch
I use Perl to perform similar instructions in which I create CSV and TXT files and its simple to achieve and very fast. I can copy/paste a small Perl program that shows how to generate files. The most commonly used one I created generates URL's for websites where pages are numerical or in an alpha order to search for info etc that is openly available on the www.Wish I could help. I can not FOLLOW what you are doing nor even guess as to why it is a CSV file. Normally CSV is used by a spreadsheet program.as way of import or export to other programs.
Have you read the Help for the FOR command? The parts of a file name have specific variables in the FOR statement. Code: [Select]@ECHO OFF CLS REM This program will generate a .csv file for import. @ECHO OFF Will hide the command flow because no one cares. set "file=output.csv" ECHO ................................................................. ECHO Welcome to the csv generating tool. Press enter to continue. ECHO ................................................................. PAUSE > nul ECHO. ECHO Make sure this program and all the files are in a single directory. ECHO It'll take the files and file path to generate a "%file%" to import. ECHO Once this operation is done, zip all the files together. ECHO. SET /P "ID=Please Input the ID Number: " ECHO Attachment Name,Type,File,Related Event > "%file%" for %%a in (*.*) do ( if /i not "%%~nxa"=="%~nx0" ( if /i not "%%~nxa"=="%file%" ( for /f "delims=" %%b in ("%%~dpa\.") do ( >>"%file%" echo "%%~nxa",4,"%%~nxb\%%~nxa",%ID% ) ) ) ) pause
Output is
Attachment Name,Type,File,Related Event "File.txt",4,"abc\File.txt",999 "ren file.bat",4,"abc\ren file.bat",999 "result.txt",4,"abc\result.txt",999 "sourceText.txt",4,"abc\sourceText.txt",999 "Target.txt",4,"abc\Target.txt",999 "Ten Twenty.txt",4,"abc\Ten Twenty.txt",999 Thanks a lot that seems to do the trick. Before being done with the issue, could you explain a little this part of the code :
Code: [Select] for %%a in (*.*) do ( if /i not "%%~nxa"=="%~nx0" ( if /i not "%%~nxa"=="%file%" ( for /f "delims=" %%b in ("%%~dpa\.") do ( >>"%file%" echo "%%~nxa",4,"%%~nxb\%%~nxa",%ID% ) ) ) )
Thanks. I really want to understand all this in case i need to unbug SOMETHING.
EDIT :
I would also like the program to be able to go fetch files into sub-directories. Let's say i have a file named test6.jpg which is inside folder "def" inside folder "abc", the csv output would include:
"test6.jpg",4,"abc\def\test6.jpg",999
Sad thing i don't fully understand the code maybe you can help me on this one also. Thanks again i really gotta say you are helping me out a lot here .I can't help with your 2nd question, but I might be able to help with understanding the code. Code: [Select]for %%a in (*.*) do ( REM This command will do everything within the "( )" for each file in the current directory
if /i not "%%~nxa"=="%~nx0" ( REM This will check to see if the file's name and extention and equal to the name and extention of the first argument ignoring case. Then will run "( )" if they are not the same.
if /i not "%%~nxa"=="%file%" ( REM This will check to see if the file's name and extention are the same as the input file, and execute "( )" if not. Ignoring case. for /f "delims=" %%b in ("%%~dpa\.") do ( REM This will then execute "( )" on the drive\pathway of the file [from the first command].
>>"%file%" echo "%%~nxa",4,"%%~nxb\%%~nxa",%ID% REM It will then write to %file% "<name.extention of file>,4,<path\name.extension>,<id>" )
)
)
)
I find it easyer to understand when it's tabbed out Code: [Select]for %%a in (*.*) do ( if /i not "%%~nxa"=="%~nx0" ( if /i not "%%~nxa"=="%file%" ( for /f "delims=" %%b in ("%%~dpa\.") do ( >>"%file%" echo "%%~nxa",4,"%%~nxb\%%~nxa",%ID% ) ) ) ) Quote from: Quardah on August 01, 2013, 05:48:35 AM I would also like the program to be able to go fetch files into sub-directories.
Next time you post here, explain the entire task, because changing the task after you have got some tested code means more work changing the code, and more testing.
Quote from: foxidrive on August 01, 2013, 10:15:26 AMNext time you post here, explain the entire task, because changing the task after you have got some tested code means more work changing the code, and more testing.
You are right and i am sorry for this. Although i did wrote up in the basic example that it could occur that files could be in different sub-folders;
From First post : txt1.txt,4,\test1\txt1.txt,999 txt2.txt,4,\test1\txt2.txt,999 txt3.txt,4,\test1\txt3.txt,999 txt4.txt,4,\test2\txt4.txt,999
We see that, in the third variable, the folder changes from test1 to test2. I should have been more clever on this and i am sorry. Don't take this as an insult i don't want to get you wrong on this, i accept the blame for not being specific enough.
All in all, as the final thing, i would like the program to be able to fetch all the files into the specified folder, which means the third variable (File) could be very short or very long depending of the tree in which the program is in.
The files may also be in root directory.
Like this :
txt1.txt,4,\txt1.txt,999 txt2.txt,4,\test1\txt2.txt,999 txt3.txt,4,\test1\test3\test5\test7\txt3.txt,999 txt4.txt,4,\test2\test8\test11\test15\test19\lemay\olddisk\backup\txt4.txt,999
I tested your code (which is great btw thanks again) and i've manage to understand the %%~nxa and %%~nxb and %%~nxc thing and so on which as far as i understand it is the filepath while %%~nxa stands for the file name and %%~nxb is the folder right before in the tree and so on but i just don't have enough experience to code the tree as a variable.
In other words, if you get into terminal and then go into that specific directory, then issue this command :
dir /a:-D /b /s
I would like the .csv to have a line for all the entries that shows up, which means those in the root directory and also those in the sub-directories (also goes on to sub-sub-directories...)
EDIT : Just so you know for double help i double thanked you
EDIT 2 : I've manage to find out that if i input "/R" right after the first "FOR" i get a recursive operation (i don't know if it's the good term but it simply means the operation is also applied to files into sub-folders)!C:\Users\Lemonilla>for /? | find "%~" | find "I" %~I - expands %I removing any surrounding quotes (") %~FI - expands %I to a fully qualified path name %~dI - expands %I to a drive letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes of file %~tI - expands %I to date/time of file %~zI - expands %I to size of file %~$PATH:I - searches the directories listed in the PATH %~dpI - expands %I to a drive letter and path only %~nxI - expands %I to a file name and extension only %~fsI - expands %I to a full path name with short names only %~dp$PATH:I - searches the directories listed in the PATH %~ftzaI - expands %I to a DIR like output line Hum yeah, i tried to set something like
IF /i "%%~nxa"=="%~nx0" ( Set "dir=%%~nxb"
Just so for the program into the file it would once set the main directory you know, and then i would have tried to tell it to input everything until he FINDS out that "dir=%%~nx(something)" it would stop.
But i failed miserably.
Help this is harder than vba Still no answer yet. I really hope somebody comes and help me finish this cause i have no clue what to do.
I just don't get the FOR function under batch. It's very hard...Asking for help by PM is not how this site works.
Now, if you show actual paths to folders and explain which folder you are launching the batch file in, and the folders you are processing then you will usually get something that works for your situation.
It's a sad fact that many people show folders and filenames that are made up - and as a result they get code that doesn't work for them, because they forget details like you did, or the filenames and foldernames are important in the batch file.The Problem becomes knowing where you want your folder structure to start for the output. It has to be constant for it to work. In other words we really need to know what the base folder is and from there we could output the correct path into your CSV. So basically the base path pretty much has to be hardcoded.
|