I have a load of personal ID numbers, which I need to download automatically. Rather than store thousands of unique personal IDs on my machine, I want to multiply all the IDs by a random integer, save the result, and delete the original file.
This will end up in Excel, but VBA is not an option because of org policy. I can't install anything - limited to what comes with Windows. As far as I understand, Excel's power query isn't able to delete a source file after reading it, but I could be wrong - if so that could be another direction to go in.
Sample of csv (the ids are not real)
136983,96771,216453,211521,589046,513320,195746,404556,190103,312261
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
413213,122904,135502,282999,460212,343970,433526,530707,,
,,,,,,,,,
,,,,,,,,,
216327,121969,165339,538695,440435,175431,396049,360505,160665,375980
,,,,,,,,,
251625,327979,155758,231189,385458,342295,365599,100644,312974,385772
,,,,,,,,,
398438,114618,216349,356734,504029,256328,466559,534053,144303,211043
306499,515840,90297,434565,,,,,,
Features
- It could be any number of rows, probably between a couple of hundred and ten thousand.
- Many of the rows are blank. Blank rows need to be preserved, because another column will need to be imported to go next to id (a blank row indicates no people correspond to this value from the imported column)
- Many of the rows have less than 10 id numbers - less than the maximum, in other words.
Desired output
Let's say that the random integer was 2, this would produce:
273966,193542,432906,423042,1178092,1026640,391492,809112,380206,624522
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
826426,245808,271004,565998,920424,687940,867052,1061414,,
,,,,,,,,,
,,,,,,,,,
432654,243938,330678,1077390,880870,350862,792098,721010,321330,751960
,,,,,,,,,
503250,655958,311516,462378,770916,684590,731198,201288,625948,771544
,,,,,,,,,
796876,229236,432698,713468,1008058,512656,933118,1068106,288606,422086
612998,1031680,180594,869130,,,,,,
What I've tried
This answer looks promising, but it only deals with one column. I couldn't figure how to adapt it so that it accepts any number of columns.
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1-18* delims=," %%A IN (mycsv.csv) DO (
set sum1="%%~C"
set /a cole=!sum1! * 2
echo %%~A,%%~B,%%~C,%%~D,!cole!
) >> output.csv
I don't really understand this Powershell based answer.
$csv = Import-Csv mycsv.csv
foreach ($row in $csv) {
[int]$row.B *= -1
[int]$row.F *= -1
}
$csv | Export-Csv output.csv
If I try to use it, it returns
powershell : Exception setting "B": "The property 'B' cannot be found on this object. Verify that the property exists and can be
At line:1 char:1
+ powershell -ep Bypass .\t.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Exception setti...sts and can be :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
set."
At C:\Users\user\OneDrive\Personal\t.ps1:3 char:3
+ [int]$row.B *= -1
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "F": "The property 'F' cannot be found on this object. Verify that the property exists and can be
set."
At C:\Users\user\OneDrive\Personal\t.ps1:4 char:3
+ [int]$row.F *= -1
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "B": "The property 'B' cannot be found on this object. Verify that the property exists and can be
set."
At C:\Users\user\OneDrive\Personal\t.ps1:3 char:3
+ [int]$row.B *= -1
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "F": "The property 'F' cannot be found on this object. Verify that the property exists and can be
set."
At C:\Users\user\OneDrive\Personal\t.ps1:4 char:3
+ [int]$row.F *= -1
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
I assumed that B and F just referred to the rows in the csv, but if I understand the error message (I may not) they're names that have to be defined (how?) Also I need this to work across all rows which have values, and skip empty rows and empty columns within rows. So hardcoding the rows and columns to be multiplied isn't going to work for me here.
Groping around in the dark, replacing
[int]$row.B *= -1
[int]$row.F *= -1
with
[int]$row *= -1
returns
powershell : Cannot convert the "@{12=18}" value of type "System.Management.Automation.PSCustomObject" to type "System.Int32".
At line:1 char:1
+ powershell -ep Bypass .\t.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Cannot convert ..."System.Int32".:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
At C:\Users\user\OneDrive\Personal\t.ps1:3 char:3
+ [int]$row *= -1
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Cannot convert the "@{12=}" value of type "System.Management.Automation.PSCustomObject" to type "System.Int32".
At C:\Users\user\OneDrive\Personal\t.ps1:3 char:3
+ [int]$row *= -1
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException