由于错误的编码或错误,无法使我的批处理脚本正常工作 [英] Can't get my Batch Script to work due to incorrect coding or bugs

查看:49
本文介绍了由于错误的编码或错误,无法使我的批处理脚本正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试操作下面列出的简单数据系列集(输入数据),以获取如下所示的输出csv文件.我一直在努力寻找错误所在以及如何编写此脚本的方法,但无法解决.

我无法运行简单的批处理脚本,当我运行批处理程序时,它什么也没做.这是我正在使用的代码,我正在使用的输入数据以及我需要获取的输出数据:

  @echo关闭setlocal enabledelayedexpansionFOR/F"tokens = 1-7 * delims ="%% A IN(eurusd1.csv)DO(设置总和1 = %%〜D-%%〜E设置/a结果=(sum!* 1000000)+1回声%%〜A,%%〜B,%%〜C,%%〜D,%%〜E,%%〜F,结果!)>>output.csv 

输入数据:

  2020.12.24,16:44,1.21906,1.21909,1.21897,1.21905,12020.12.24,16:45,1.21904,1.21910,1.21889,1.21906,12020.12.24,16:46,1.21905,1.21913,1.21899,1.21909,1 

希望输出数据看起来像这样:

  2020.12.24,16:44,1.21906,1.21909,1.21897,1.21905,1202020.12.24,16:45,1.21904,1.21910,1.21889,1.21906,2112020.12.24,16:46,1.21905,1.21913,1.21899,1.21909,141 

解决方案

  @ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONrem源目录,目标目录,目标目录的以下设置,rem批处理目录,文件名,输出文件名和临时文件名[如果显示]是名称我用于测试的rem,故意包含名称,其中包含空格以确保提醒您,该过程使用此类名称即可正常工作.这些将需要更改以适合您的情况.设置"sourcedir = u:\您的文件"设置"destdir = u:\您的结果"SET" filename1 =%sourcedir%\ q65529783.csv"设置"outfile =%destdir%\ outfile.csv"(FOR/f"usebackqtokens = 1-7delims ="%% a IN(%filename1%")DO(FOR/f"tokens = 1-4delims =".%% s IN("%% d.%% e")DO SET/a值=(%% s * 100000 ^)+ 1 %% t-(%% u * 100000 ^)-1 %% v&SET/a值= 10 *值+1回声%% a,%% b,%% c,%% d,%% e,%% f ,!值!))>%outfile%"转到:EOF 

我使用了一个名为 q65529783.csv 的文件,其中包含您的数据用于测试.

产生定义为%outfile%

的文件

仅需使用 usebackq 选项,因为我选择在源文件名周围添加引号.

首先,使用-7列进行读取和标记化,因此标记1-7,尽管最后一个未使用.

接下来,使用.作为分隔符,重新标记字符串 %% d.%% e .请注意 %% d %% e 之间的点.

现在设置我们的价值.由于没有迹象表明所涉及的值范围(我假设它们都为正),因此我们将 %% d 乘以 %% s 即可将其转换为六位数.代码> 100000,然后添加 1 %% t .这样可以解决 %% t 包含前导0(否则将被视为八进制)的情况.

冲洗并重复 %% e 并执行减法,请注意, %% v 之前的 1 之前的平衡%% t .需要插入符号,以便 cmd )视为 set 的一部分,而不是 for 的一部分.

由于结果现在是10000 *浮点数差,因此请按照原始代码的预期乘以10并加1.

并输出结果.

由于您没有解释为什么操作的第一个结果是 120 ,对于公式而言,这似乎是不合逻辑的,因此,我假设这是一个错误,应该是 121.

I'm trying to manipulate a simple data series set (input data) as listed below to get an output csv file as shown below. I have been pulling my hair out trying to work out where the bugs are and how to code this script, but could not work it out.

I can't get my simple batch script to work, when I run the batch program it does nothing. Here is the code I'm using, the input data I'm using, and the output data that I need to obtain:

@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1-7* delims=," %%A IN (eurusd1.csv) DO (
    set sum1 = %%~D - %%~E
    set /a result=(sum!*1000000)+1
    echo %%~A,%%~B,%%~C,%%~D,%%~E,%%~F,!result!
) >> output.csv

Input data:

2020.12.24,16:44,1.21906,1.21909,1.21897,1.21905,1
2020.12.24,16:45,1.21904,1.21910,1.21889,1.21906,1
2020.12.24,16:46,1.21905,1.21913,1.21899,1.21909,1

Want output data to look like this:

2020.12.24,16:44,1.21906,1.21909,1.21897,1.21905,120
2020.12.24,16:45,1.21904,1.21910,1.21889,1.21906,211
2020.12.24,16:46,1.21905,1.21913,1.21899,1.21909,141

解决方案

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q65529783.csv"
SET "outfile=%destdir%\outfile.csv"

(
FOR /f "usebackqtokens=1-7delims=," %%a IN ("%filename1%") DO (
 FOR /f "tokens=1-4delims=." %%s IN ("%%d.%%e") DO SET /a value=(%%s*100000^)+1%%t-(%%u*100000^)-1%%v&SET /a value=10*value+1
 ECHO %%a,%%b,%%c,%%d,%%e,%%f,!value!
)
)>"%outfile%"

GOTO :EOF

I used a file named q65529783.csv containing your data for my testing.

Produces the file defined as %outfile%

The usebackq option is only required because I chose to add quotes around the source filename.

First, read and tokenise using , - 7 columns, so tokens 1-7, although the last is unused.

Next, re-tokenise the string %%d.%%e using . as a delimiter. Note the dot between %%d and %%e.

Now set our value. Since there is no indication of the value-ranges involved (I have assumed that they are all positive) then we convert %%d to a six-digit number by multiplying %%s by 100000 and adding 1%%t to that. This takes care of the situation where %%t contains a leading 0 which would otherwise be treated as octal.

Rinse and repeat for %%e and perform the subtraction, noting that the 1 preceding %%v balances that preceding %%t. The carets are required so that cmd sees the ) as part of the set, not the for.

Since the result is now 10000* the floating-point difference, multiply by 10 and add 1, as anticipated in the original code.

and output the result.

Since you don't explain why the first result of the manipulation is 120, which seems illogical given the formula, I'll assume that this is an error and it should be 121.

这篇关于由于错误的编码或错误,无法使我的批处理脚本正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆