python-3.xbatch-filecommand-linecsvkit

CSVKIT: in2csv Batch Convert on Windows 7


I have been CSVKIT on my mac, but I am moving my workflow to a PC platform so others will be able to access my workflow. One of the items I have been using was in2csv for converting Fixed Width file formats. On the mac I have it setup for batch processing:

for x in $(ls desktop/fixedwidth/*.txt); do x1=${x%%.*}; in2csv -f fixed -s desktop/ff/schema.csv $x > $x1.csv; echo "$x1.csv done."; done

I have tried converting this to a PC command-line option that I can run from a batch file, but i haven't had any luck

for /r %%i in (*.csv) do c:\programdata\python\python36\scripts\in2csv -f fixed -s c:\users\username\desktop\in2cs_schema\test_schema.csv c:\users\username\desktop\test_files\*.txt > c:\users\username\desktop\test_files\*.csv %%i

If I run the command by itself it works without an issue:

c:\programdata\python\python36\scripts\in2csv -f fixed -s [SCHEMA PATH] [FIXEDWIDTH PATH] > [OUTPUT PATH]

I feel like I am missing something simplistic, any help would be greatly appreciated!


Solution

  • This should use a standard FOR statement

    FOR %i in (c:\users\%username%\desktop\test\*.txt) DO (in2csv -f fixed -s c:\users\%username%\desktop\schema\schema.csv c:\users\%username%\desktop\test\%~ni.txt > c:\users\%username%\desktop\test\%~ni.csv)
    

    If you need to run a batch process for this command then you would need to add %%VARIABLE%% to all the variable items; ex. %%i or %%username%%.

    Also, to get just the filename in the work you can use %~n would return solely the filename within any extension.